Ruby on rails Rails-将注释作为部分内容进行编辑
我正在使用Rails构建一个事件应用程序,在事件显示页面的底部有一个评论部分。我希望用户能够创建/更新(编辑)/删除他们自己的评论,同时保持在同一显示页面上。我该怎么做 我已经编写了一些代码,但我对rails还是相当陌生的,我的代码试图将用户从show页面中带走,创建一个“comments”show页面,而不仅仅是在events show页面上编辑表单。我有正确的模型关联has\u many/归属于,我的注释嵌套在我的事件路由中。这是到目前为止我的代码- 评论\u controller.rbRuby on rails Rails-将注释作为部分内容进行编辑,ruby-on-rails,ruby,ruby-on-rails-4,model-view-controller,Ruby On Rails,Ruby,Ruby On Rails 4,Model View Controller,我正在使用Rails构建一个事件应用程序,在事件显示页面的底部有一个评论部分。我希望用户能够创建/更新(编辑)/删除他们自己的评论,同时保持在同一显示页面上。我该怎么做 我已经编写了一些代码,但我对rails还是相当陌生的,我的代码试图将用户从show页面中带走,创建一个“comments”show页面,而不仅仅是在events show页面上编辑表单。我有正确的模型关联has\u many/归属于,我的注释嵌套在我的事件路由中。这是到目前为止我的代码- 评论\u controller.rb
class CommentsController < ApplicationController
def create
@event = Event.find(params[:event_id])
@comment = @event.comments.create(params[:comment].permit(:user_id, :body))
redirect_to event_path(@event)
end
def show
@comment = Comment.find(params[:id])
@comment = @event.comments.find(params[:id])
end
def edit
@comment.user = current_user
end
def update
if @comment.update(comment_params)
redirect_to event_path(@event)
else
render 'edit'
end
end
def destroy
@event = Event.find(params[:event_id])
@comment = @event.comments.find(params[:id])
@comment.destroy
redirect_to event_path(@event)
end
private
def comment_params
params.require(:comment).permit(:body, :event_id, :user_id)
end
end
# some code for Events show...
# Comments code -
<% if user_signed_in? %>
<div id="comments">
<%= render 'comments/form', commentable: @event %>
<% if @event.comments.any? %>
<h2><%= @event.comments.size %> Comment</h2>
<%= render @event.comments %>
<% else %>
<h2>There are no comments yet</h2>
<% end %>
</div>
<% end %>
<%= simple_form_for([commentable, Comment.new ]) do |f| %>
<%= f.label :comment, label: 'Add a comment' %><br>
<%= f.text_area :body %><br>
<br>
<%= f.button :submit, "Create", class: "btn btn-primary" %>
<% end %>
class CommentsController
Event.show.erb
class CommentsController < ApplicationController
def create
@event = Event.find(params[:event_id])
@comment = @event.comments.create(params[:comment].permit(:user_id, :body))
redirect_to event_path(@event)
end
def show
@comment = Comment.find(params[:id])
@comment = @event.comments.find(params[:id])
end
def edit
@comment.user = current_user
end
def update
if @comment.update(comment_params)
redirect_to event_path(@event)
else
render 'edit'
end
end
def destroy
@event = Event.find(params[:event_id])
@comment = @event.comments.find(params[:id])
@comment.destroy
redirect_to event_path(@event)
end
private
def comment_params
params.require(:comment).permit(:body, :event_id, :user_id)
end
end
# some code for Events show...
# Comments code -
<% if user_signed_in? %>
<div id="comments">
<%= render 'comments/form', commentable: @event %>
<% if @event.comments.any? %>
<h2><%= @event.comments.size %> Comment</h2>
<%= render @event.comments %>
<% else %>
<h2>There are no comments yet</h2>
<% end %>
</div>
<% end %>
<%= simple_form_for([commentable, Comment.new ]) do |f| %>
<%= f.label :comment, label: 'Add a comment' %><br>
<%= f.text_area :body %><br>
<br>
<%= f.button :submit, "Create", class: "btn btn-primary" %>
<% end %>
#一些事件代码显示。。。
#注释代码-
评论
目前还没有评论
注释。_comment.html.erb
<div class="comment clearfix">
<div class="comment_content">
<p class="comment_user"><strong><%= comment.user %></strong></p>
<p class="comment_body"><%= comment.body %></p>
<p class="comment_time"><%= time_ago_in_words(comment.created_at) %> Ago</p>
</div>
<% if user_signed_in? and current_user %>
<p><%= link_to 'Delete', [comment.event, comment],
method: :delete,
class: "button",
data: { confirm: 'Are you sure?' } %></p>
<p><%= link_to 'Edit', [comment.event, comment] %> </p>
<% end %>
</div>
以前
评论\u form.html.erb
class CommentsController < ApplicationController
def create
@event = Event.find(params[:event_id])
@comment = @event.comments.create(params[:comment].permit(:user_id, :body))
redirect_to event_path(@event)
end
def show
@comment = Comment.find(params[:id])
@comment = @event.comments.find(params[:id])
end
def edit
@comment.user = current_user
end
def update
if @comment.update(comment_params)
redirect_to event_path(@event)
else
render 'edit'
end
end
def destroy
@event = Event.find(params[:event_id])
@comment = @event.comments.find(params[:id])
@comment.destroy
redirect_to event_path(@event)
end
private
def comment_params
params.require(:comment).permit(:body, :event_id, :user_id)
end
end
# some code for Events show...
# Comments code -
<% if user_signed_in? %>
<div id="comments">
<%= render 'comments/form', commentable: @event %>
<% if @event.comments.any? %>
<h2><%= @event.comments.size %> Comment</h2>
<%= render @event.comments %>
<% else %>
<h2>There are no comments yet</h2>
<% end %>
</div>
<% end %>
<%= simple_form_for([commentable, Comment.new ]) do |f| %>
<%= f.label :comment, label: 'Add a comment' %><br>
<%= f.text_area :body %><br>
<br>
<%= f.button :submit, "Create", class: "btn btn-primary" %>
<% end %>
要在保持在同一页面的同时执行操作,您需要添加remote:true
提交到您的表单并通过JS提交。您可以在Rails/JS文档中阅读更多关于这方面的内容。
然后,标准的铁路方式是引导您到另一条路线。
要禁止这样做,并在同一个查看页面上执行所有操作,可以使用JS
要将操作限制到当前用户,您需要确保注释属于当前用户。差不多
如果用户已登录?和comment.user_id==当前_user.id
(以防万一你上面的代码不是防弹的。)注释代码/表格在哪里?我不会再帮你了。上次我花了很多时间在你身上,你说这对你很有用,但是0投赞成票或被标记为正确!你指的是哪个问题?