Saturday, September 4, 2010

[Rails] Re: Cannot seem to display Comments on User Show Page

It would help to know the URL your being redirected to... but I see
one potential issue:

if @comment.save
flash[:success] = "Comment saved."
redirect_to articles_path(@article)
else
render 'show'
end

Lets pretened your comment failed to save... your rendering show...
Show what? By default your going to render the comment#show action
with no ID. You want to be rendering articles/show i assume.

On Sep 4, 1:48 pm, Kelp Kelp <li...@ruby-forum.com> wrote:
> Hello,
> I have added a comment form to my article show page, but after I submit
> the comment, I get the following error: Couldn't find Article without an
> ID.
> I think it is because the Comments controller does not know which
> article to add the new comment.
>
> comments_controller:
>   def create
>     @article = Article.find(params[:article_id])
>     @comment = @article.comments.create(params[:comment])
>     if @comment.save
>       flash[:success] = "Comment saved."
>       redirect_to articles_path(@article)
>     else
>       render 'show'
>     end
>   end
>
> article show page:
> <% @article.comments.each do |comment| %>
>   <%= comment.title %>
>   <%= comment.content %>
>   <%= comment.user %>
> <% end %>
>
> <%= form_for([@article, @article.comments.build]) do |f| %>
>   <%= render 'shared/error_messages', :object => f.object %>
>   <div class="field">
>     <%= f.label :title %><br />
>     <%= f.text_field :title %>
>   </div>
>   <div class="field">
>     <%= f.label :content %><br />
>     <%= f.text_area :content %>
>   </div>
>   <div class="actions">
>     <%= f.submit "Post Comment" %>
>   </div>
> <% end %>
>
> I am trying to follow this guide:http://edgeguides.rubyonrails.org/getting_started.html#adding-a-secon...
>
> Thanks.
> --
> Posted viahttp://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate