Too many components?


#1

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?


#2

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?


#3

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


#4

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


#5

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