复选框参数未创建关联模型



我有一些复选框,单击这些复选框将更新关联的模型。强参数允许这些参数,但不更新模型。

"配置文件"模型

有一行"伦敦"模型。

<form role="form" action="/profiles" method="post">
  <div class="form-group">
    <input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
    <input class="form-control" type="text" name="profile[bio]">
      <div class="checkbox">
        <label> <input type="checkbox" name="london[south]" value="south">South London</label>
      </div>
      <div class="checkbox">
        <label> <input type="checkbox" name="london[central]" value="central">Central London</label>
      </div>
      <div class="checkbox">
        <label> <input type="checkbox" name="london[north]" value="north">North London</label>
      </div>
      <div class="checkbox">
        <label> <input type="checkbox" name="london[west]" value="west">West London</label>
      </div>
      <div class="checkbox">
        <label> <input type="checkbox" name="london[east]" value="east">East London</label>
      </div>

    </div>      
    <button type="submit" class="btn btn-default">Submit</button>
  </div>
</form>

我的参数显示在 rails 服务器日志中:

"profile"=>{"bio"=>"}, "london

"=>{"South"=>"South", "Central"=>"Central", "North"=>"North"}}

我的控制器:

  class ProfilesController < ApplicationController
  def new
    @profile = Profile.new
  end
  def create
      @profile = Profile.new(profile_params)
      if @profile.save 
        redirect_to '/profiles' 
      else
        render '/profiles'
      end
  end
  private
  def profile_params
    params.require(:profile).permit(:bio, londons_attributes: [:south, :north, :east, :west, :central])
  end
end

尝试使用

def profile_params
    params[:profile].permit!
end

最新更新