0

我有一个VendorProduct模型。

:vendor has_many :products:product belongs_to :vendor。外键product应在模型上。

我的routes.rb样子是这样的:

resources :products 
resources :vendors do
  resources :products
end

我的链接标签Vendor#Show看起来像这样:

<%= link_to 'Add New Product', new_vendor_product_path(@vendor) %>

我的Product#Create控制器如下所示:

  def create
    if params[:vendor_id]
        @vendor = Vendor.find(params[:vendor_id])
        @product = @vendor.products.build(params[:product])
        @vendor.save
    else
            @product = Product.new(params[:product])
    end
.
..
.
#everything else is normal from the scaffold

这是我单击以下链接时日志的外观new_vendor_product_path(@vendor)

Started GET "/vendors/4/products/new" for 127.0.0.1 at 2012-12-07 02:06:44 -0500
Processing by ProductsController#new as HTML
  Parameters: {"vendor_id"=>"4"}
  Vendor Load (0.2ms)  SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" = ? LIMIT 1  [["id", "4"]]
  Rendered products/_form.html.erb (8.1ms)
  Rendered products/new.html.erb within layouts/application (8.8ms)
  Rendered layouts/_navigation.html.erb (0.5ms)
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
   (0.2ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
  Rendered layouts/_login_nav.html.erb (4.1ms)
  Rendered layouts/_header.html.erb (5.8ms)
  Rendered layouts/_messages.html.erb (0.1ms)
  Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 39ms (Views: 35.8ms | ActiveRecord: 0.6ms)

这是POST我提交之后:

Started POST "/products" for 127.0.0.1 at 2012-12-07 02:07:09 -0500
Processing by ProductsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"hwssA+7v5O4TzqhSk=", "product"=>{"name"=>"Women's Dri-Fit", "description"=>"Dri Fit, Dri Fit what do you see.", "image"=>#<ActionDispatch::Http::Upla0 @original_filename="Nike-Dri-FIT-Tech-Womens-Running-Tights.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[image]\"; filename=\"Nike-Dri-FIT-Tech-Womens-Running-Tights.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/fold-2056-janl07>>, "price"=>"24.99"}, "commit"=>"Create Product"}
   (0.1ms)  begin transaction
  SQL (0.7ms)  INSERT INTO "products" ("created_at", "description", "image", "name", "price", "updated_at", "vendor_id") VALUES (?, ?, ?, ?, ?, ?, ?)  [["created_at", Fri, 07 Dec 2012 07:07:10 UTC +00:00], ["description", "Dri Fit, Dri Fit what do you see."], ["image", "Nike-Dri-FIT-Tech-Womens-Running-Tights.jpg"], ["name", "Women's Dri-Fit"], ["price", 24.99], ["updated_at", Fri, 07 Dec 2012 07:07:10 UTC +00:00], ["vendor_id", nil]]
   (3.7ms)  commit transaction
Redirected to http://localhost:3000/products/14
Completed 302 Found in 9619ms (ActiveRecord: 4.4ms)

注意vendor_id => nil.

编辑1:

<%= simple_form_for @product, :html => {:multipart => true} do |f| %>
    <%= f.error_notification %>

    <div class="form-inputs">
      <div class="span8">
          <%= f.input :name, :label => "Product Name", :wrapper_html => { :class => "span6" } %>
      </div>
      <div class="span8">
          <%= f.input :description, :as => :text, :wrapper_html => { :class => "span6" }, :input_html => { :rows => 5 } %>
      </div>
      <div class="span8">
          <%= f.input :image, :label => "Upload Image", :wrapper_html => { :class => "span6" } %>    
      </div>
      <div class="span8">
          <%= f.input :price, :wrapper_html => { :class => "span6" } %>
      </div>
      <div class="span8">
          <%= f.button :submit, :class => "btn", :wrapper_html => { :class => "span6" } %>
      </div>
    </div> <!-- /.form-inputs -->

    <!-- <div class="form-actions">

    </div> -->
  <% end %>
4

2 回答 2

2

只需将其添加到您的表单中:

 <%= f.hidden_field :vendor_id %>

它将使用供应商 ID 创建一个隐藏字段,因此当您提交表单时它不会为零。

于 2012-12-07T07:59:37.520 回答
0

原来问题是我必须将我的_form部分修改为:

<%= simple_form_for [@vendor, @product], :html => {:multipart => true} do |f| %>

而不仅仅是@product作为第一个参数。

完成后,我将控制器修改为:

if @vendor
    @product = @vendor.products.build(params[:product])
else
    @product = Product.new(params[:product])
end

效果非常好!

于 2012-12-08T02:45:57.847 回答