使用从数据库生成的radio_button的Form_for

时间:2013-04-01 03:30:56

标签: forms devise ruby-on-rails-3.2 has-many-through

这看起来很容易,基本上我将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

为什么我收到此错误?

1 个答案:

答案 0 :(得分:0)

如果我理解正确的话,如果您构建Trade而不是Figure,那么此错误就会消失(您只是想通过新的交易将数字链接到用户, ?):

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