表单参数已发送,但其中一个在控制器中被接收为nil

时间:2019-06-05 22:58:40

标签: ruby-on-rails

注册模型的目的是将另外三个模型 gymsite 用户角色分组。

class Gymsite
has_many :users

class User
has_many :roles, through: :assigments
has_many :assigments

class Assigment
belongs_to :user
belongs_to :role

class Role
has many :assigments
has_many :users, through: :assigments

从app / views / user / new.html.erb上的表单,输入值将发送到app / controllers / user_controller.rb以执行操作“注册”。问题是所有的enroll_params都按浏览器中的显示方式发送,但在控制器上,其中一个以循环方式变为nil。

我尝试通过不同的方式进行尝试,包括将不同的表单字段作为select和check_boxes来尝试解决此问题,但没有结果。我很想知道问题的根源是什么以及如何解决它。谢谢

浏览器处理信息:

Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0z2B2t7H5wYJjtJMn8zBIPJ7sJaFk706EPh5JVZsC76x  GWeggrJi7RSxAu8GJJmsyrNP7NzVv6rNvmumNDyhsg==", "enroll"=>  {"user_first_name"=>"Lorena", "user_last_name"=>"Cueva",   "user_email"=>"lcueva@example.com", "user_password"=>"[FILTERED]",   "user_password_confirmation"=>"[FILTERED]", "gymsite_id"=>"1",   "u_rol"=>["", "2", "4"]}, "commit"=>"Enroll User"}

app / views / users / new.html.erb

<%= simple_form_for @enroll, url: users_path do |form| %>

<div class='row'>
<div class='columns medium-2'></div>
   <div class='columns medium-4'>
   <%= form.input :user_first_name %>
  <%= form.input :user_last_name %>
  <%= form.input :user_email %>
  <%= form.input :user_password %>
  <%= form.input :user_password_confirmation %>
  </div>
  <div class='columns medium-4'>
  
  <%= form.input :u_rol, as: :check_boxes, collection: Role.all.pluck(:name, :id),  input_html: {multiple: true}%>

  <%= form.submit 'Enroll User'%>
  <% end %>
  </div>
</div>
<%= link_to 'Back', users_path %>

在app / controllers / users_controller.rb

def new
    @enroll = Enroll.new
    @roles = Role.all
  end

def create
   @enroll = Enroll.new(enroll_params)
   if @enroll.save
     redirect_to root_path
   else
     render :new
   end
 end

def enroll_params
      params.require(:enroll).permit(:user_email, :user_password, :user_password_confirmation, :gymsite_id, :u_rol, :user_first_name, :user_last_name)
end

在app / models / enroll.rb中,所有参数均正确执行,但u_rol变为nil除外。

class Enroll
  include ActiveModel::Model

    attr_accessor :user_email, :user_password, :user_password_confirmation, :gymsite_id, :u_rol, :user_first_name, :user_last_name, :user, :role

     def save
       if valid?
         #find gymsite by gymsite_id
         gym = Gymsite.find(gymsite_id)
         #save user for a gymsite
         @user = gym.users.create(first_name: user_first_name, last_name: user_last_name, email: user_email, password: user_password, password_confirmation: user_password_confirmation )
         #assign roles to the user
           u_rol.each do |n|
            rol = Role.find(n)
            @user.roles << rol
          end
        end
    end

1 个答案:

答案 0 :(得分:0)

我解决了将enroll_parms更改为

的问题
 def enroll_params
      params.require(:enroll).permit(:user_first_name, :user_last_name,:user_email, :user_password, :user_password_confirmation, :gymsite_id, u_rol: [])
    end

在app / views / users / new.html.erb中,添加了'include_hidden: false”以摆脱空白值:

 <%= form.input :u_rol, as: :check_boxes, collection: Role.all.pluck(:name, :id), include_hidden: false, input_html: {multiple: true}%>

现在可以了!谢谢@jvillian!