下拉列表中的Rails关联

时间:2017-05-17 01:28:13

标签: ruby-on-rails ruby

我正在使用集合选择来显示网站上的所有类别。

<%= form.collection_select :category_id, Category.all, :id, :name, :prompt => "Select category" %>

我还在许可证参数中包含了category_id

def portfolio_params
  params.require(:portfolio).permit(:name, :client, :completed, :about,:square, :cover, :category_id)
end

不应该使用像这样的

这样的创建方法形成关联
 def create
  @portfolio = Portfolio.new(portfolio_params)
  @test = portfolio_params[:category_id]
  respond_to do |format|
   if @portfolio.save
    format.html { redirect_to @portfolio, notice: 'Portfolio was successfully created.' }
    format.json { render :show, status: :created, location: @portfolio }
  else
    format.html { render :new }
    format.json { render json: @portfolio.errors, status: :unprocessable_entity }
  end
end

但是当我查看测试变量时,它显示为null。我真的对此感到困惑。我正在使用rails 5和ruby 2.3.0

这是params

<ActionController::Parameters 
  {"utf8"=>"✓", 
   "authenticity_token"=>"XzyeAtZF5mOXG2GdRIrlmRcPwhruAOfrvzIWTD9QUsIJ4GMytB6s38oM3NT9RIwr+B8osJWasmg2Qd0VoROTiw==", 
   "portfolio"=><ActionController::Parameters
     {"name"=>"test", 
      "client"=>"", 
      "completed(1i)"=>"2017", 
      "completed(2i)"=>"5", 
      "completed(3i)"=>"17", 
      "about"=>"", 
      "categoy_id"=>"3"} permitted: false>, 
   "commit"=>"Create Portfolio", 
   "controller"=>"portfolios", 
   "action"=>"create"} permitted: false>

0 个答案:

没有答案