1

这似乎很容易做到,基本上我将 Devise 用于管理目的,因此每个注册用户都可以创建一个链接到他们的帐户的产品/图形,然后将该产品与另一个用户进行交易。

Class User
  rolify
  :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :role_ids, :as => :admin
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_many :trades
  has_many :figures, :through => :trades
  accepts_nested_attributes_for :trades

Class Figure
  attr_accessible :image_url, :name, :series, :figure_id

  has_many :trades
  has_many :users, :through => :trades
  accepts_nested_attributes_for :trades

Class Trade
  attr_accessible :figure_id, :user_id

  belongs_to :user
  belongs_to :figure

贸易管制员

def new
  @trade = Trade.new
end

def create
  @trade = current_user.figures.build(params[:trade])
end

贸易表格

<%= form_for(@trade) do |f| %>
  <div class="field">
    <% Figure.all.each do |figure| %>
      # Create a list of figurines radio buttons
      <%= f.radio_button :figure_id, figure.id, :checked => (params[:figure_id] == nil ? false : params[:figure_id]) %>
        # Link the figures thumbnail to the radio button
      <%= f.label :figure_id, image_tag(figure.image_url, :size => "40x58", :alt => figure.name ), :value => "#{figure.id}" %>
   <% end %>
 </div>

 <div class="actions">
   <%= f.submit %>
 </div>
<% end %>

这是参数

{"utf8"=>"✓",
  "authenticity_token"=>"lo+RWOLxGhPIP1pmJhk9v+vetO7cGEKGY844egaQ6A0=",
  "trade"=>{"figure_id"=>"12"},
  "commit"=>"Create Trade"}

这是我遇到的问题:未知属性:figure_id

为什么我会收到此错误?

4

1 回答 1

0

Trade如果我对您的理解正确,如果您构建 a而不是 a ,则此错误应该消失Figure(您只是试图通过新交易将数字链接到用户,对吗?):

def create
   @trade = current_user.trades.build(params[:trade])
   @trade.save
end
于 2013-04-01T03:46:01.517 回答