У меня есть две модели: сообщение и комментарий. Комментарий является вложенным ресурсом поста:
routes.rb:
resources :posts do
resources :comments
end
Я даю пользователям возможность редактировать комментарии, которые отображаются в представлении пост-шоу :
сообщений / show.hmtl.erb:
<%= render @comments %>
комментарии / _comment.html.erb:
<%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %>
Эта форма:
комментарии / _form.html.erb:
<h4>Add a comment:</h4>
<%= form_for([@post, @post.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% unless current_user == nil %>
<% if current_user.id == @post.user_id %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<% end %>
<% end %>
<%= link_to 'Back', posts_path %>
Предназначен для создания комментария в представлении шоу.
Мне нужна другая форма для редактирования комментария в новом шаблоне:
комментарии / edit.html.erb:
<h1>Edit comment</h1>
<%= render 'form2' %>
<%= link_to 'Back', posts_path %>
комментарии / form2.html.erb:
<h4>Edit comment:</h4>
<%= form_for() do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', posts_path %>
Не уверен, что здесь разместить:
<%= form_for(HERE) do |f| %>
Есть предложения?
EDIT
comments_controller.rb:
class CommentsController < ApplicationController
def show
@comment = Comment.find(params[:id])
end
def new
@comment = Comment.new
end
def edit
@comment = Comment.find(params[:id])
end
def update
@post = Post.find(params[:post_id])
@comment = Comment.find(params[:id])
@comment.update_attributes(params[:comment])
redirect_to @post
end
def create
@post = Post.find(params[:post_id])
comment_attr = params[:comment].merge :user_id => current_user.id
@comment = @post.comments.create(comment_attr)
redirect_to post_path(@post)
end
end