我的表单没有保存我的category_id外键

时间:2014-12-22 00:07:23

标签: ruby-on-rails ruby ruby-on-rails-4 simple-form nested-resources

我是rails的新手,我有一个表单,可以在类别模型中选择相关类别的选项来保存供应商单位。但是当我尝试点击保存所有保存的字段但category_id没有。起初错误是未经许可的参数但是当我尝试将category_id属性放入强参数时,错误被删除但不幸的是它没有保存agian。

我已经在stackoverflow和google中搜索了任何内容,但是没有解决方案我尝试解决它。​​

supplierunit模式

belongs_to :category
belongs_to :supplierprofile

类别模型

has_many :supplierunits

Supplierprofile模式

belongs_to :supplier
has_many   :supplierunits

_form.html.erb

<%= simple_form_for([@supplier, @unit]) do |f| %>
  <%= f.association :category, collection: Category.all.collect{ |p| [p.category_name, p.id] } %>
  <%= f.input       :unit_name %>
  <%= f.input       :unit_address      %>
  <%= f.text_area   :description  %>
  <%= f.button :submit %>
<% end %>

Supplierunit controller

    before_action :set_unit , only: [:new, :create]

    def new
        @unit = @supplier.supplierunits.new
    end

   def create
        @unit = @supplier.supplierunits.new(unit_params)

        if @unit.save
            flash[:notice] = "Successfully Added!"
        else
            render 'new'
        end
   end

    private

    def set_unit
        @supplier = Supplierprofile.find_by_supplier_id(session[:user_id])
    end

    def unit_params
        params.require(:supplierunit).permit(:unit_name, :unit_address, :description, category_id: :category_id)
    end
end

谢谢!

0 个答案:

没有答案