Editing a nested Link_to rails form
I just followed the next tutorial and did a great job. http://www.communityguides.eu/articles/6
Anyway, it's hard for me, and it's editing.
I called my link_to edit after
<%= link_to 'Edit', edit_article_comment_path(@article, comment) %>
Which will lead me to an error page and not sure why.
NoMethodError in Comments#edit
Showing /home/jean/rail/voyxe/app/views/comments/_form.html.erb where line #1 raised:
undefined method `comment_path' for #<#<Class:0xa8f2410>:0xb65924f8>
Extracted source (around line #1):
1: <%= form_for(@comment) do |f| %>
2: <% if @comment.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
Now here is the form in the comments edit
<%= form_for(@comment) do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here is the controller The controller of the article shows
@article = Article.find(params[:id])
@comments = @article.comments.find(:all, :order => 'created_at DESC')
Editing the comment controller
def edit
@comment = Comment.find(params[:id])
end
source to share
It looks like it comment
is a nested resource, so you need to specify article
which it contains comment
. For example:
<%= form_for [@article, @comment] do |f| %>
comment_path
is an undefined method because there is no route that provides top-level comments. It is sometimes useful to use rake routes
to find out what routes are available.
UPDATE:
The article you link only provides tags create
and delete
for comments. If you need to support edit operations, you will need to change routes by changing:
resources :comments, :only => [:create, :destroy]
in
resources :comments, :only => [:create, :destroy, :edit, :update]
You will also need to implement edit and update actions - once the agreement is resolved, the form will be displayed and the update will handle the form submission. You also need to make sure it @article
is available in your edit mode.
source to share