【问题标题】:Rails - ActionController::UrlGenerationError No route matches missing required keys: [:id]Rails - ActionController::UrlGenerationError 没有路由匹配缺少必需的键:[:id]
【发布时间】: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.erb partial?

标签: ruby-on-rails ruby model-view-controller


【解决方案1】:

根据你的代码,如果你没有循环@comments

comment 应该是@comment

<% 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 %>

另外,将您的编辑操作更改为,

   def edit
        @event = @comment.event
        respond_to do |f|
            f.js 
            f.html 
        end
    end

【讨论】:

  • 我已经尝试过了,但它不起作用 - 它试图指向一个 id,但我不知道为什么。
  • 你能粘贴你的路由文件吗?
  • 上面刚刚加的。
  • 我仍然遇到同样的错误。这可以连接到通过 simple_form 传递的参数吗?
  • 这就是我所拥有的 -
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-09
  • 2015-11-26
相关资源
最近更新 更多