Forum: Ruby on Rails too many components?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
44c645f2c3a008972d9904102bae09df?d=identicon&s=25 matt (Guest)
on 2006-02-24 21:27
(Received via mailing list)
okay.

on /posts/show/13

i have show.rhtml, which does
render_component(:controller=>'comments', :action=>'display_comments',
:id=>post.id)

on comments/display_comments

i iterate through the comments and then, display_comments.rhtml

render_component(:controller=>'comments', :action=>'new', :id=>@id)

(@id is the post_id)

so this is all works okay, i can add the comment and i changed the
redirect to posts/show/@id

problem is, if the new comment form fails (say, i dont' put in a
required author field), it redirects to /comments/new/ to give me an
error message when i really wnat that error message to be back on
/posts/show/

so, two part question -- how can i resolve that issue above?

and, am i using too many components?  is there a simpler, more elegant
way i could be doing this?
44c645f2c3a008972d9904102bae09df?d=identicon&s=25 matt (Guest)
on 2006-02-24 23:31
(Received via mailing list)
my question is perhaps too vague.

basically, i have a component form with some validation requirements.

problem is, when that form validation fails and bounces back, it goes to
the form action /comments/new/ rather than the file that rendering that
component (/posts/show/:id)

how can i get the form to bounce the validation errors to the main file?
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-02-25 10:05
(Received via mailing list)
What does your action that handles the form submission look like? I'd
bet that you're
doing the redirect to the success view whether there were errors or not.

b
44c645f2c3a008972d9904102bae09df?d=identicon&s=25 matthew collins (Guest)
on 2006-02-27 23:08
(Received via mailing list)
sorry for the delay on this, and thanks for the help:



on success, it should (and does) redirect to posts_controller.rb
(/posts/show/id

but if validation fails, it goes back to itself (/comments/create) when
i'd rather it go to /posts/show/id

#this create method is in the comments_controller.rb*
#it is being included as a component by /posts/show/id

def create
    @comment = Comment.new(params[:comment])
       @id = params[:id]
        @comment.post_id = @id

    if @comment.save
      flash[:notice] = 'Comment was successfully created.'
      redirect_to :controller=>'posts', :action => 'show', :id=>@id
    else
      render :action => 'new'
    end
  end
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-02-28 07:34
(Received via mailing list)
matthew collins wrote:
 > on success, it should (and does) redirect to posts_controller.rb
 > (/posts/show/id
 >
 > but if validation fails, it goes back to itself (/comments/create)
when
 > i'd rather it go to /posts/show/id
 >
 > #this create method is in the comments_controller.rb*
 > #it is being included as a component by /posts/show/id
 >
 > def create
 >    @comment = Comment.new(params[:comment])
 >       @id = params[:id]
 >        @comment.post_id = @id
 >      if @comment.save
 >      flash[:notice] = 'Comment was successfully created.'
 >      redirect_to :controller=>'posts', :action => 'show', :id=>@id
 >    else
 >      render :action => 'new'
 >    end
 >  end

Yeah, notice that the "else" on the "if @comment.save" goes to your
"new" action if your
validation fails. Instead, you could switch it to always redirect to
/posts/show/id.

The side benefit of this is that you never leave the input url (the
action attribute of
the form tag) in the address bar of the browser. However, since a
redirect is a new
request, you'll lose the validation errors, so you gotta stick them in
the flash... I just
stick the model object in there and pull it out again in the view:

  def create
     @comment = Comment.new(params[:comment])
     @post = Post.find(params[:id])
     @comment.post = @post
     if @comment.save
       flash[:notice] = 'Comment was successfully created.'
     else
       flash[:comment] = @comment
     end
     redirect_to :controller=>'posts', :action => 'show', :id=>@id
   end

and in the posts page:

<% @comment = @flash[:comment]
     if(not @comment.nil?)
%>
<%= error_messages_for(:comment)%>
<%  end %>

Note that I switched your setting of the post_id on comment to
retrieving the post and
setting the object (@comment.post = @post). I think that's the more
rails-ish way of doing it.

Now, I did almost exactly this in my app, but I'm not using any
components... so I don't
know if that'll make a difference. Wouldn't think so though...

b
This topic is locked and can not be replied to.