【问题标题】:Form_for with radio_button generated from database带有从数据库生成的单选按钮的 Form_for
【发布时间】:2013-03-22 03:54:35
【问题描述】:

这似乎很容易做到,基本上我将 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

为什么我会收到这个错误?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多