【发布时间】:2017-05-22 10:20:24
【问题描述】:
我在我的 Rails 应用程序中尝试对 Comments MVC 进程实施编辑功能时遇到上述错误。
这是出现错误的视图代码 -
_cmets.html.erb
<% if user_signed_in? %>
<p><%= link_to "Edit", edit_event_comment_path(@event, comment), remote: true %></p>
<p><%= link_to 'Delete', [comment.event, comment],
method: :delete,
class: "button",
data: { confirm: 'Are you sure?' } %></p>
<% end %>
这是 cmets 控制器代码 -
Comments_controller.rb
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
def create
@event = Event.find(params[:event_id])
@comment = @event.comments.create!(params[:comment].permit(:name, :body))
@comment.user_id = current_user.id
redirect_to @event
end
# GET /comments/1/edit
def edit
respond_to do |f|
f.js
f.html
end
end
def update
respond_to do |format|
if @comment.update
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.js { }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
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
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:event_id, :body)
end
end
我已将 cmets 设置为事件的嵌套路由,因此路由路径是正确的。我已经尝试了一些不同的变体作为反复试验,但我仍然遇到错误。我确信答案正盯着我看,但我需要另一双眼睛的帮助。任何帮助表示赞赏。
【问题讨论】:
-
试试
link_to "Edit", edit_event_comment_path(event_id: @event.id, id: @comment.id) -
TL;DR 但参数哈希可能有
:event_id或:comment_id而不是:id -
你在哪个文件中渲染
_comment.html.erbpartial?
标签: ruby-on-rails ruby model-view-controller