1

だから私はこのエラーを持っています:

Routing Error

No route matches {:action=>"show", :controller=>"products", :list_id=>#<Product id: 1, name: "a", model: "", description: "", version: "", company: "", price: nil, image_source: nil, created_at: "2012-12-02 18:43:26", updated_at: "2012-12-02 18:43:26">}

:list_id=>#... は少しおかしくなっていることに注意してください。なんとかProduct入りました。

ここにproduct.rbがあります

class Product < ActiveRecord::Base
  attr_accessible :company, :description, :model, :name, :price, :version

  has_many :list_product_interactions
  has_many :lists, :through => :list_product_interactions

  validates_uniqueness_of :name, :scope => [:version]

ここにlist.rbがあります

class List < ActiveRecord::Base
  attr_accessible :activity, :description, :title

  has_many :list_product_interactions
  has_many :products, :through => :list_product_interactions, :order => "list_product_interactions.votes_up DESC"

ここに list_product_interactions.rb があります

class ListProductInteraction < ActiveRecord::Base
  attr_accessible :list_id, :product_id, :votes_activity, :votes_down, :votes_total, :votes_up

  belongs_to :list
  belongs_to :product

製品の複製を作成するとエラーが発生します (既に使用されている名前の製品)。エラーが発生するコントローラーは次のとおりです。

products_controller.rb

class ProductsController < ApplicationController
  def create
    @list = List.find(params[:list_id])
    @product = @list.products.build(params[:product])
    if @list.save
      raise "ok"
      redirect_to list_path(@list)
    else
      @list.reload
      params[:list_id] = @list.id
      render template: "lists/show"
    end
  end

  def show
    @product = Product.find(params[:id])
  end
end

どうすればデバッグできますか? ありがとう

更新:lists_controller.rbを追加

class ListsController < ApplicationController
  def index
    @lists = List.all
  end

  def new
    @list = List.new
  end

  def create
    @list = List.new(params[:list])
    if @list.save
      redirect_to @list
    else
      render :new
    end
  end

  def show
    @list = List.find(params[:id])
    @product = @list.products.build
    nil
  end
end

リスト表示テンプレート

h1
  = @list.title
br
= @list.description
hr
- @list.products.each do |product|
  - if !product.id.nil?
    = link_to product.name, list_product_path(product), class: "product_link"
    '
    = product.version
    '
    = link_to "up:", "/lists/#{@list.id}/products/#{product.id}/vote_up", :method => :put
    = product.product_up_votes(@list.id)
    '
    = link_to "down:", "/lists/#{@list.id}/products/#{product.id}/vote_down", :method => :put
    = product.product_down_votes(@list.id)

hr
h3 Add products:
= form_for([@list, @product]) do |f|
  - if @product.errors.any?
    |product errors
    = pluralize(@product.errors.count, "error")
    ul
    - @product.errors.full_messages.each do |msg|
      li
        = msg
  br
  = f.label :name
  = f.text_field :name
  br
  = f.label :version
  = f.text_field :version
  br
  = f.submit
hr
= link_to "All lists", lists_path
4

2 に答える 2

0

:list_idパラメータにバグがある値:

Routing Error
No route matches {:action=>"show", :controller=>"products", 
  :list_id=>#<Product id: 1, name: "a", model: "", description: "", version: "", company: "", price: nil, image_source: nil, created_at: "2012-12-02 18:43:26", updated_at: "2012-12-02 18:43:26">}

/config/routes.rbこれはルーティングエラーであるため、おそらくからの出力を投稿する必要がありますrake routes

私の知る限り、あなたはネストされたリソースルートに依存しているようです。

#/config/routes.rb
resources :lists do
  resources :products
end

それが本当なら、クリチャードはおそらく正しいので、あなたは使うべきです

list_product_path(:id => product.id, :list_id => @list.id )
list_product_path(product,@list)     #this should also work

との両方を渡さないproduct.idlist.id、リクエストはネストされたリソースルートと一致しません

于 2012-12-09T09:53:16.170 に答える
0

ruby-debug で Rails をデバッグする


Rails で使用されるデバッガー ruby​​-debug は gem として提供されます。インストールするには、次を実行します。

sudo gem install ruby-debug
# if you run 1.9
sudo gem install debugger

次に、次のようなことができます

<% debugger;true %>
# or in the controller/model/lib
def somemethode
    # ... 
    debugger
    # ...
end

インタプリタがdebuggerステートメントに到達すると停止し、コンソールで現在の環境をインタラクティブに探索できます。

あなたの問題


...おそらくここにあります:

    = link_to product.name, list_product_path(product), class: "product_link"

これは、list_product_path がリスト オブジェクトと対応する製品を必要とする可能性が高いためlist_product_path(@list,product)です。lists_products_path(product)

于 2012-12-02T19:53:31.197 に答える