# Too many components?

**URL:** <https://www.ruby-forum.com/t/too-many-components/54164>\
**Category:** Rails\
**Created:** [February 24, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/too-many-components/54164 "2006-02-24T21:27:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [February 24, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/too-many-components/54164/1 "2006-02-24T21:27:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [February 24, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/too-many-components/54164/2 "2006-02-24T23:31:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [February 25, 2006, 10:05am UTC](https://www.ruby-forum.com/t/too-many-components/54164/3 "2006-02-25T10:05:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [February 27, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/too-many-components/54164/4 "2006-02-27T23:08:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [February 28, 2006, 7:34am UTC](https://www.ruby-forum.com/t/too-many-components/54164/5 "2006-02-28T07:34:58Z")

</div>

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
