2

2 つのネストされたリソースがあります。

class Customer < ActiveRecord::Base
  has_many :locations, :dependent => :destroy
  accepts_nested_attributes_for :locations
end

class Location < ActiveRecord::Base
  belongs_to :customer
end

routes.rb には

resources :customers do
  resources :locations
end    

新しい場所を作成するためのコードは次のとおりです。

<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>

新しい場所を作成しようとすると、次のエラーが表示されます

ルーティング エラー

{:action=>"show", :controller=>"locations", :customer_id=>#, :id=>#} に一致するルートはありません

「new」の代わりに :action=>「show」が呼び出されるのはなぜですか?

rake routes 出力は

customer_locations     GET    /customers/:customer_id/locations(.:format)                  {:action=>"index", :controller=>"locations"}
                       POST   /customers/:customer_id/locations(.:format)                  {:action=>"create", :controller=>"locations"}
new_customer_location  GET    /customers/:customer_id/locations/new(.:format)              {:action=>"new", :controller=>"locations"}
edit_customer_location GET    /customers/:customer_id/locations/:id/edit(.:format)         {:action=>"edit", :controller=>"locations"}
customer_location      GET    /customers/:customer_id/locations/:id(.:format)              {:action=>"show", :controller=>"locations"}
                       PUT    /customers/:customer_id/locations/:id(.:format)              {:action=>"update", :controller=>"locations"}
                       DELETE /customers/:customer_id/locations/:id(.:format)              {:action=>"destroy", :controller=>"locations"}

location_controller.rb の新しいアクションのコントローラー コードは次のとおりです。

before_filter :find_customer

def new
  @location = @customer.locations.new

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @location }
  end
end

エラーがわかりません。正しく動作する別の 2 つのネストされたリソースがあり、すべてのコードをチェックしましたが、まったく同じように見えます... ?

4

2 に答える 2

1

わかりました、問題が見つかりました。

app/views/locations/new.html.erb によって呼び出される app/views/locations/_form.html.erb 内

間違ったパス ヘルパーのリンクがありました:

    <%= link_to "Cancel", customer_location_path(@customer,@location) %>

に変更しました

    <%= link_to "Cancel", customer_path(@customer) %>

そして今、すべてが機能します

于 2011-12-22T04:26:44.013 に答える
0

image_tag に閉じ括弧を付けてみてください。

<%= link_to image_tag("add.png"), new_customer_location_path(@customer), :class => "button" %>
于 2011-12-21T13:18:44.077 に答える