Ruby on rails 我应该在此表单中放置什么变量来编辑注释(Rails)?
我有两个模型:Post和Comment。Comment是Post的嵌套资源: routes.rb:Ruby on rails 我应该在此表单中放置什么变量来编辑注释(Rails)?,ruby-on-rails,Ruby On Rails,我有两个模型:Post和Comment。Comment是Post的嵌套资源: routes.rb: resources :posts do resources :comments end <%= render @comments %> <%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %> <h4>Add a comment:</h4&
resources :posts do
resources :comments
end
<%= render @comments %>
<%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %>
<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 %>
<h1>Edit comment</h1>
<%= render 'form2' %>
<%= link_to 'Back', posts_path %>
<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 %>
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
我使用户能够编辑在“后期放映”视图中显示的评论:
posts/show.hmtl.erb:
resources :posts do
resources :comments
end
<%= render @comments %>
<%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %>
<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 %>
<h1>Edit comment</h1>
<%= render 'form2' %>
<%= link_to 'Back', posts_path %>
<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 %>
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
comments/\u comment.html.erb:
resources :posts do
resources :comments
end
<%= render @comments %>
<%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %>
<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 %>
<h1>Edit comment</h1>
<%= render 'form2' %>
<%= link_to 'Back', posts_path %>
<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 %>
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
此表格:
评论/\u form.html.erb:
resources :posts do
resources :comments
end
<%= render @comments %>
<%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %>
<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 %>
<h1>Edit comment</h1>
<%= render 'form2' %>
<%= link_to 'Back', posts_path %>
<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 %>
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
添加注释:
|
用于在“显示”视图中创建注释
我需要另一个表单在新模板中编辑注释:
comments/edit.html.erb:
resources :posts do
resources :comments
end
<%= render @comments %>
<%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %>
<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 %>
<h1>Edit comment</h1>
<%= render 'form2' %>
<%= link_to 'Back', posts_path %>
<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 %>
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
编辑注释
comments/form2.html.erb:
resources :posts do
resources :comments
end
<%= render @comments %>
<%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %>
<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 %>
<h1>Edit comment</h1>
<%= render 'form2' %>
<%= link_to 'Back', posts_path %>
<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 %>
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
编辑注释:
不确定在此处放置什么:
<%= form_for(HERE) do |f| %>
有什么建议吗
编辑
评论\u controller.rb:
resources :posts do
resources :comments
end
<%= render @comments %>
<%= link_to "Edit Post Comment", edit_post_comment_path(@post, comment) %>
<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 %>
<h1>Edit comment</h1>
<%= render 'form2' %>
<%= link_to 'Back', posts_path %>
<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 %>
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
class CommentsControllercurrent\u user.id
@comment=@post.comments.create(comment\u attr)
将_重定向到post_路径(@post)
结束
结束
注释是嵌套的资源,这意味着注释始终与特定的帖子绑定。在编辑操作中,您将获得comment_id和post_id作为参数。您应该通过这些id加载评论和帖子。不要在表单中生成注释,以便在新建和编辑操作中重用表单。将您的表格更改为:
评论/_form.html.erb
<%= form_for([@post, @comment]) do |f| %>
def edit
@comment = Comment.find(params[:id])
@post = Post.find(params[:post_id])
end
并且在posts\u controller.rb中
def show
@post = Post.find(params[:id])
@comment = @post.comments.build
end
注释是嵌套的资源,这意味着注释总是绑定到特定的帖子。在编辑操作中,您将获得comment_id和post_id作为参数。您应该通过这些id加载评论和帖子。不要在表单中生成注释,以便在新建和编辑操作中重用表单。将您的表格更改为: 评论/_form.html.erb
<%= form_for([@post, @comment]) do |f| %>
def edit
@comment = Comment.find(params[:id])
@post = Post.find(params[:post_id])
end
并且在posts\u controller.rb中
def show
@post = Post.find(params[:id])
@comment = @post.comments.build
end