未定义的方法`merge' for:name:Symbol

时间:2017-08-31 10:14:11

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

我正在尝试使用表单中的选择框。当我这样尝试时

<%= s_form.select :experience_category_id, ExperienceCategory.all, :name,:id%> 

它给了我错误

  

未定义的方法合并:name:Symbol。

可能是因为我必须使用签名s_form.collection_select。但它不允许我多次选择。

当我这样尝试时

<%= s_form.select(:experience_category_id, ExperienceCategory.all {|p| [ p.name, p.id ] },
                                             { :prompt => "Please select"},
                                             { :multiple => true, :size => ExperienceCategory.all.count } )%>

Supplier_controller

def new
    @user = User.new
    @user.build_supplier
    @user.supplier.build_natural_address
  end
def create
    @user = User.new(user_params)
    @user.role = "supplier"

      if @user.save
        UserMailer.account_activation(@user).deliver_now
        flash[:notice] = "Please check your email to activate your account"
        redirect_to root_url
      else
        render :new
      end

  end

。 。

def user_params
      params.require(:user).permit(:email, :first_name, :last_name, :password, :password_confirmation, supplier_attributes: [:id, :company_name, :insurance_document, :profile_image, :national,:experience_category_id, natural_address_attributes: [:addressable_id, :addressable_scope, :addressable_type, :address_line_1, :address_line_2, :address_line_3, :town, :county, :postcode]])
    end

创建了供应商我无法在服务器中看到任何问题,但没有:experience_catagory_id。 它不会引发错误,但:experience_category_id仍然存在nil。可能是什么问题?我该如何解决这个问题?谢谢

3 个答案:

答案 0 :(得分:2)

this link是您问题的良好示例参考

如果您想选择一个选项

,请查找您的问题
<%= s_form.collection_select :experience_category_id, ExperienceCategory.all, :id, :name, :prompt => 'Select' %>

进行多项选择

<%= s_form.collection_select :experience_category_ids, ExperienceCategory.all, :id, :name, {}, {multiple: true'} %>

在您的控制器中请注意特别是在您的strong_parameters中它应该是一个数组

def user_params
  params.require(:user).permit(
    :your_other_fields,
    :experience_category_ids => []
end

答案 1 :(得分:1)

<%= f.select(:experience_category_id, ExperienceCategory.find(:all).collect {|u| [u.name, u.id]}, :prompt => 'Select') %>

答案 2 :(得分:0)

你没有按照rails html help

使用正确的格式

使用此

<%= f.select(:product_category_id, product.find(:all).collect {|u| [u.name, u.id]}, :prompt => 'Select') %>

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {include_blank: 'None'})

在文档中 Rails helpers