【问题标题】:assign a model's attribute through association通过关联分配模型的属性
【发布时间】:2012-08-31 03:36:57
【问题描述】:

我是 Rails 新手,正在开发 Rails 应用程序,我一直在思考这个问题。

我有三个模型

class Product < ActiveRecord::Base

    attr_accessible :name, :issn, :category, :user_products_attributes

    validates_presence_of :name, :issn, :category
    validates_numericality_of :issn, :message => "has to be a number"

    has_many :user_products
    has_many :users, :through => :user_products

    accepts_nested_attributes_for :user_products

end




class UserProduct < ActiveRecord::Base

  attr_accessible :price, :category

  validates_presence_of :price, :category
  validates_numericality_of :price, :message => "has to be a number"

  belongs_to :user
  belongs_to :product

end

class user < ActiveRecord::Base

  # devise authentication here

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_many :user_products, :dependent => :destroy
  has_many :products, :through => :user_products

end

产品控制器

定义新

@product = product.new

@product.user_products.build 

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

结束

所以问题是这样的:我希望用户在表单中输入产品的信息,但它还涉及输入与产品相关联的不同模型/表 (user_product) 中存在的产品价格.我怎样才能做到这一点?你可以看到我的 form_for 使用了@product。

我们将不胜感激。

    <div class="span8">

       <div id="listBoxWrapper">          
        <fieldset>

         <%= form_for(@product, :html => { :class => "form-inline" }, :style => "margin-bottom: 60px" ) do |f| %>


            <div class="control-group">
              <label class="control-label" for="Category">Category</label>
              <div class="controls">

                <%= f.text_field :category, :class => 'input-xlarge', :id => "Category" %>       

              </div>
            </div>

           <div class="control-group" style="display: inline-table;">
           <label class="control-label" for="First Name">Price($/Month)</label>
             <div class="controls">
              <%= product.fields_for :user_products do |p| %>
                <%= p.text_field :price, :class => 'input-xlarge input-name' %>    
                <% end %>
              </div>
            </div>

【问题讨论】:

  • 我按照您的步骤和 railscast 教程进行操作。我已经编辑了上面的代码...现在我收到“未知属性:book_id”错误...我似乎认为我的 user_products 的命名约定错误...谢谢!
  • 对不起,我的意思是 product_id...我想我现在可以工作了...我刚刚在我的 user_products 表中添加了一个 product_id...
  • 一切正常...非常感谢!!!....不过我正在重新考虑我设置所有模型的方式..可能不是最好的方式...如果你在乎,我可以解释,也许您可​​以提供更好的方法。再次感谢
  • 你为什么不...让我们看看:)
  • 好酷...我会输入一些内容并在大约一个小时后发送...我可能需要展开另一个讨论。再次感谢

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

它应该是一种跟随

<%= form_for(@product) do |product| %>
  <%= product.text_field :name, :class => 'input-xlarge input-name' %>
  <%= product.fields_for :user_products do |user_product| %>
      <%= user_product.text_field :price, :class => 'input-xlarge input-name' %>        

您需要在控制器中为@product 构建您的user_products。喜欢

@product.user_products.build

你的Product 模型应该有以下内容

accepts_nested_attributes_for :user_products

这将使它意识到user_products 值可能会在保存产品实体时出现。

编辑:

只是解释嵌套形式的骨架

form_for(@object) do |object_form_builder|
       # call any field generator helper function by object_form_builder like
       object_form_builder.text_field
       object_form_builder.check_box
       # so on...

       #Now for nested forms get the nested objects from the builder like
       object_form_builder.fields_for :nested_objects do |nested_object_builder|
             #generate the fields for this with nested_object_builder. Like
             nested_object_builder.text_field
             # so on...
       end
 end

是的,为对象构建器使用短名称总是很方便,就像您通常使用 f 代替 form_builder 一样。

【讨论】:

  • 好的,我正在使用您的解决方案。但是我收到一个错误,说 product.fields_for 中的产品是一个未定义的局部变量或方法......还有什么是使用构建的目的?谢谢
  • 您能粘贴您的form 代码吗?因为如果product.text_field 可以运行,那么product.fields_for 没有理由失败。 build 是告诉@product i may need to generate fields for these entities also 的机制。看看当你只想生成@product 的形式时,你通过Product.new 构建产品。但是在这里你还需要@product.user_products 的字段。所以你必须建立它。
  • 我修复了错误。页面已加载,但价格的 text_field 未显示。
  • 我会尝试使用productuser_product(或类似于模型实例的东西)在您的表单中保留类似f 的块变量或简单的东西有点令人困惑。在他发布的更新中,您可以看到他使用了product.fields_for 而不是f.fields_for
  • @justcode 你需要“建立”一个user_product 如果你还没有一个已经存在的,否则它不会显示任何东西。设置产品后,您可以在控制器中执行此操作,@product.user_products.build
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多