2012-05-04 194 views
0

我對軌道相當新,我不認爲我完全理解路由。當我嘗試訪問編輯動作我得到以下錯誤:Rails嵌套的資源路線

ActiveRecord::RecordNotFound in StoreController#show 

Couldn't find Gear with id=edit 
Rails.root: /Users/dave/rails_projects/outdoor 

Application Trace | Framework Trace | Full Trace 
app/controllers/store_controller.rb:7:in `show' 
Request 

Parameters: 

{"user_id"=>"104", 
"id"=>"edit"} 
Show session dump 

Show env dump 

Response 

Headers: 

None 

這是我與引發此錯誤的鏈接查看:

<li><%= link_to "Store Appearance", edit_user_store_path(@user) %></li> 

這裏是我的嵌套的路線:

resources :users do 
    resources :store 
    end 

這裏是我的控制器

class StoreController < ApplicationController 
    def index 
    @store = current_user.gears.paginate(page: params[:page]) 
    end 

    def show 
    @gears = Gear.find(params[:id]).user.gears.paginate(page: params[:page]) 
    end 

    def edit 
    end 

    def update 
    end 
end 

模範店

class Store < ActiveRecord::Base 
    attr_accessible :storeimage, :storename 
    belongs_to :user 

    validates :user_id, :presence => true 

end 

型號用戶

class User < ActiveRecord::Base 

    attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :userimage, :remove_userimage 
    has_secure_password 
    has_many :gears 
    has_many :comments, :dependent => :destroy 
    has_one :store, :dependent => :destroy 
    before_save :create_remember_token 
    require 'carrierwave/orm/activerecord' 
    mount_uploader :userimage, UserpicUploader 

    email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

    validates :first_name, presence: true, 
          length: {:maximum => 50 } 
    validates :last_name, presence: true, 
         length: {:maximum => 50 } 
    validates :email,  presence: true, 
         format: {:with => email_regex}, 
         uniqueness: {:case_sensitive => false} 
    validates :password, presence: true, 
         confirmation: true, 
         length: {within: 6..40} 

    include Tire::Model::Search 
    include Tire::Model::Callbacks 

    def name 
    first_name + " " + last_name 
    end 

    private 

    def create_remember_token 
     self.remember_token = SecureRandom.urlsafe_base64 
    end 

end 

請幫助。

回答

1

你需要在URL同時通過user_idid PARAMS當你訪問下user嵌套在store對象,所以您的網址應該是這樣的:

/users/1/stores/3/edit 

對戰:

/users/1/stores/edit 

您還需要將這兩個參數作爲參數傳遞給您的路徑幫助程序,即:

edit_user_store_path(@user, @store) 
+0

好吧,如果我改變了包含@store參數的路徑。如何在給定模型has_one和belongs_to關係的控制器中定義該模型? – DaveG

+0

定義控制器中的內容? – coreyward

+0

對不起,我應該更清楚。我不確定如何定義@store變量。我明白了,謝謝你的幫助 – DaveG