has_many belongs_to关系不通过params

时间:2014-10-15 23:24:39

标签: ruby-on-rails ruby one-to-many nested-attributes has-many

我正在做一个小商店管理员

Product.rb

  class Product < ActiveRecord::Base
        has_many :product_options
         accepts_nested_attributes_for :product_options
    end 

ProductOption.rb

class ProductOption < ActiveRecord::Base
    belongs_to :product
end

products_controller.rb

class Admin::ProductsController < AdminApplicationController 

    def index
        @products = Product.all

    end

    def new
        @product = Product.new
    end

    def create
        @product = Product.new(product_params)
        if @product.save 
            redirect_to admin_products_path
        end
    @product_option = @product.product_options.create(params[:product_option])
  end

  def edit 
    @product = Product.find(params[:id])
  end

  def update
    @product = Product.find(params[:id])
    if @product.update(product_params)
        redirect_to admin_products_path
    end
  end

  def destroy
    @product = Product.find(params[:id])
    @product.destroy
    flash[:notice] = "#{@product.name} has been deleted."
      redirect_to admin_products_path
  end

  def upload
    uploaded_io = params[:id]
    File.open(Rails.root.join('public', 'product_pics', uploaded_io.original_filename), 'wb') do |file|
      file.write(uploaded_io.read)
  end
end

private
    def product_params
        params.require(:product).permit(:name, :product_id, :position, :product_description, :product_image_type, :product_image, :product_detail, :product_option_id, :option_name, :product_option )
    end
end

product_option_controller.rb

class Admin::ProductOptionsController < AdminApplicationController 

    def index
    @product_options = ProductOption.all
    end

    def new
        @product_option = ProductOption.new
    end

    def create
        @product_option = ProductOption.new(product_option_params)
    end

    def show
        @product_option = ProductOption.find(params[:id])
    end
end

private
    def product_option_params
        params.require(:product_option).permit(:option_name, :ranking, :total_redeemed, :product_id)
    end
end

_form.html.erb

<%= simple_form_for([:admin, @product] , :html => {:multipart => true})  do |f| %>
  <section class="main_content-header">
    <div class="main_content-header-wrapper">
      <nav class="main_content-breadcrumbs">
        <ul class="breadcrumbs">
          <li><%= link_to "All Products", admin_products_path %></li>
          <h1> Edit Product </h1>
        </ul>
      </nav>

      <div class="main_content-header-save">
         <%= link_to "Cancel", admin_products_path, id: "main_content-header-save-cancel" %>
         <%= f.submit %>
      </div>
    </div>
  </section>
  <div class="main_content-section">
    <section class="main_content-section">
      <div class="main_content-section-area">
        <%= f.input :name %>
        <%= f.input :product_description %>
        <%= f.input :product_detail %>
        <%= f.file_field :product_image %>
        <p> If this product has options, enter them below</p>
        <%= f.simple_fields_for :product_option, @product_option do |option_form| %>
          <%= option_form.input :option_name %>  
        <% end %>    
      </div>
    </section>
  </div>
<% end %>

服务器输出:...一直说:不允许使用product_option

Started POST "/admin/products" for 127.0.0.1 at 2014-10-15 16:13:25 -0700
Processing by Admin::ProductsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"t96EMVlDND42HuVUzxWuss2bYDVhBokieTqN2Gz3N9I=", "commit"=>"Create Product", "product"=>{"name"=>"cvncvbn", "product_description"=>"cvbn", "product_detail"=>"", "product_option"=>{"option_name"=>"cvnbnvcb"}}}
Unpermitted parameters: product_option
  SQL (1.5ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `products` (`created_at`, `name`, `product_description`, `product_detail`, `updated_at`) VALUES (?, ?, ?, ?, ?)  [["created_at", "2014-10-15 23:13:25"], ["name", "cvncvbn"], ["product_description", "cvbn"], ["product_detail", ""], ["updated_at", "2014-10-15 23:13:25"]]
   (0.4ms)  COMMIT
Redirected to http://localhost:3000/admin/products
  SQL (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `product_options` (`product_id`) VALUES (?)  [["product_id", 119]]
   (0.3ms)  COMMIT
Completed 302 Found in 10ms (ActiveRecord: 2.8ms)


Started GET "/admin/products" for 127.0.0.1 at 2014-10-15 16:13:25 -0700
Processing by Admin::ProductsController#index as HTML
  Product Load (0.3ms)  SELECT `products`.* FROM `products`
  Rendered admin/products/index.html.erb within layouts/admin (11.7ms)
  User Load (0.3ms)  SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 7  ORDER BY `users`.`id` ASC LIMIT 1
  Rendered admin/_header.html.erb (1.4ms)
  Rendered admin/_nav.html.erb (0.4ms)
Completed 200 OK in 21ms (Views: 19.8ms | ActiveRecord: 0.6ms)

记录得到保存,因此在产品选项表中只有product_id,..但没有其他参数....在过去6小时内尝试过一百万件事...所以我真的没有列表所有可能的选择,...但如果有人能看到一个明显的错误,你的智慧将不胜感激。

-------------------------------------------- ------------------------------------------------

我想通了,我没有正确使用accepts_nested_attributes这些是我必须做的所有工作的改变。

- 删除了product_options控制器(不需要) - 改变了product_params:

private
    def product_params
        params.require(:product).permit(:name, :product_id, :position, :product_description, :product_image_type, :product_image, :product_detail, :product_option_id, 
        :product_options_attributes => [:id, :option_name, :ranking, :total_redeemed, :product_id])
    end
end

- 从产品控制器的创建操作

中删除此行
 @product_option = @product.product_options.create(params[:product_option])

- 将此行添加到产品控制器的新操作

 @product.product_options.build 

- 在此循环中添加了“:product_option”(并删除了“@product_option”)

    <%= f.simple_fields_for :product_option, @product_option do |option_form| %>
      <%= option_form.input :option_name %>  
    <% end %> 

主要的变化是添加了S ......没有它,根本没有调用嵌套属性

1 个答案:

答案 0 :(得分:1)

尝试类似

的内容
def product_params
  params.require(:product).permit(:name, :product_id, :position, :product_description, :product_image_type, :product_image, :product_detail, :product_option_id, :option_name, product_option: [:option_name] )
end