【问题标题】:has_many belongs_to relationship not passing through paramshas_many belongs_to 关系不通过参数
【发布时间】:2014-10-15 23:24:39
【问题描述】:

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

产品.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 小时内尝试了一百万件事...所以我真的没有清单在所有可能的选择中,.. 但如果有人能看到明显的错误,您的智慧将不胜感激。

------------------------------------------ ------------------------------------------------强>

我想通了,我没有正确使用 accept_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”添加了一个s(并删除了“@product_option”)

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

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多