2014-11-20 60 views
0

我在提交表單後收到奇怪的錯誤。一直在努力解決這個問題了幾個小時..嵌套的資源參數沒有在form_for中正確設置

No route matches {:action=>"show", :controller=>"items", :item_id=>"141", :matter_id=>"3"} missing required keys: [:id]

的參數是:

{"utf8"=>"✓", 
"authenticity_token"=>"w0D7XmX2X2/ZMU19T6RlMvWCEClXnCFFOR+4EdIFvWg=", 
"comment_item"=>{"item_id"=>"", 
"name"=>"kaljdf", 
"body"=>"yet another comment test"}, 
"commit"=>"Post Comment", 
"matter_id"=>"3", 
"item_id"=>"141"} 

我有以下型號:

class Matter < ActiveRecord::Base 
    has many :discoveries 
    delegate :items, to: :discoveries 
end 

class Discovery < ActiveRecord::Base 
    belongs_to :matter 
    scope :items, -> { where(type: 'Item') } 
end 

class Item < Discovery 
    has_many :comment_items 
end 

class CommentItem < ActiveRecord::Base 
    belongs_to :item 
end 

控制器:

class ItemsController < DiscoveriesController 
    def show 
    @item = Item.find(params[:id]) 
    @comment_item = CommentItem.new 
    end 

    def edit 
    @item = Item.find(params[:id]) 
    end 

    def new 
    @item = Item.new 
    end 
end 

class CommentItemsController < ApplicationController 
    before_action :set_comment_item, only: [:show, :edit, :update, :destroy] 

    def new 
    @item = Item.find(params[:item_id]) 
    @comment_item = @item.comment_item.new 
    end 

    def create 
    @item = Item.find(params[:item_id]) 
    @comment_item = @item.comment_items.new(comment_item_params) 
    if @comment_item.save 
     flash[:notice] = 'Comment was successfully created' 
     redirect_to matter_item_url(matter_id: params[:matter_id]) 
    else 
     flash[:notice] = "Error creating comment: #{@comment.errors}" 
     redirect_to matter_item_url(@matter, @item) 
    end 
    end 

    def destroy 
    @comment_item = CommentItem.find(params[:id]) 
    @comment_item.destroy 
    redirect_to(@comment_item.item) 
    end 

    private 
    def set_comment_item 
     @comment_item = CommentItem.find(params[:id]) 
    end 

    def comment_item_params 
     params.require(:comment_item).permit(:name, :body, :item_id, :matter_id) 
    end 
end 

該項目資源的表演動作:

<p> 
    <strong>Matter:</strong> 
    <%= @item.matter_id %> 
</p> 

<p> 
    <strong>Content:</strong> 
    <%= @item.content %> 
</p> 
<hr /> 
<%= form_for @comment_item, url: matter_item_comment_items_path(matter_id: @item.matter, item_id: @item.id) do |f| %> 
    <% if @comment_item.errors.any? %> 
    <ul> 
     <% @comment_item.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
    </ul> 
    <% end %> 
    <%= f.hidden_field :item_id %> 

    <p> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </p> 

    <p> 
    <%= f.label :body %><br /> 
    <%= f.text_area :body %> 
    </p> 

    <p> 
    <%= f.submit "Post Comment" %> 
    </p> 
<% end %> 
<%= render :partial => 'comment_item', :collection => @item.comment_items %> 

<%= link_to 'Edit', edit_matter_item_path(id: @item.id) %> | 
<%= link_to 'Back', matter_items_path %> 

路線

resources :items do 
    resources :comment_items 
end 

resources :matters do 
    resources :items do 
    resources :comment_items 
    end 
end 

當在控制檯CommentItems看,我看到的評論實際上添加模型與他們正確的ID,但他們似乎並沒有作爲參數傳遞..我錯過了什麼?

我查看Rails 4 form_for double nested commentsRails 3.2 - Nested Resource Passing ID,但我並沒有太多的運氣..

我真的很感謝您的幫助!

+0

您的請求將會上述ItemsController代替CommentItemsController – 2014-11-21 01:02:28

+0

謝謝!你在哪裏看到? – casekey 2014-11-21 02:03:30

回答

0

No route matches {:action=>"show", :controller=>"items", :item_id=>"141", :matter_id=>"3"} missing required keys: [:id]

您的請求將會上述ItemsController代替CommentItemsController

見:控制器=> 「項目」