0

I am new in rails and I'm trying to make a form to associate roles with users, I have two collection_select in the form but when I want to create or update I get this error:

enter image description here

my controller code is :

class UserRolesController < ApplicationController
 before_action :set_user_role, only: [:show, :edit, :update, :destroy]
 @@roles =RolesController.new 
 @@users =UsersController.new 
 # GET /user_roles
 # GET /user_roles.json
 def index
 @user_roles = UserRole.all
 end

 # GET /user_roles/1
 # GET /user_roles/1.json
 def show
 end

 # GET /user_roles/new
 def new
  @user_role = UserRole.new
  @users =@@users.get_all_users
  @roles =@@roles.get_all_roles
 end

 # GET /user_roles/1/edit
 def edit
   @users =@@users.get_all_users
   @roles =@@roles.get_all_roles
   @user_roles =UserRole.find(params[:id])
   @selected_role=@user_roles.role_id
   @selected_user=@user_roles.user_id  
end

# POST /user_roles
# POST /user_roles.json
def create
  @user_role = UserRole.new(user_role_params)
  respond_to do |format|
   if @user_role.save
        format.html { redirect_to @user_role, notice: 'User role was   successfully created.' }
        format.json { render :show, status: :created, location: @user_role }
   else
     format.html { render :new }
     format.json { render json: @user_role.errors, status: :unprocessable_entity }
   end
 end
end



 # PATCH/PUT /user_roles/1
 # PATCH/PUT /user_roles/1.json
 def update
   respond_to do |format|
     if @user_role.update(user_role_params)
       format.html { redirect_to @user_role, notice: 'User role was successfully updated.' }
       format.json { render :show, status: :ok, location: @user_role }
    else
       format.html { render :edit }
       format.json { render json: @user_role.errors, status: :unprocessable_entity }
    end
  end
end

 # DELETE /user_roles/1
 # DELETE /user_roles/1.json
   def destroy
   @user_role.destroy
   respond_to do |format|
   format.html { redirect_to user_roles_url, notice: 'User role was successfully destroyed.' }
   format.json { head :no_content }
  end
 end

 def get_user_role_by_userid(user_id)
   return UserRole.where(user_id:user_id).first
 end
 private
   # Use callbacks to share common setup or constraints between actions.
   def set_user_role
    @user_role = UserRole.find(params[:id])
  end

   # Never trust parameters from the scary internet, only allow the white list through.
  def user_role_params
    params.require(:user_role).permit(:role_id,:user_id)
  end

end

My form code is :

<%= form_for(user_role) do |f| %>
  <% if user_role.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(user_role.errors.count, "error") %> prohibited this user_role from being saved:</h2>

      <ul>
        <% user_role.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= f.label :user %>
    <%= collection_select(:user_id, :user_id,@users, :id, :name,{:selected => @selected_user}) %>
  </div>

  <div class="field">
    <%= f.label :rol %>
    <%= collection_select(:role_id, :role_id,@roles, :id, :role {:selected => @selected_role}) %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

What i am doing wrong? , how i have to set the user_role_params in this case? I am using ruby 2.3.1 and Rails : 5.0.1

Thanks for your suggestions.

Andres
  • 389
  • 1
  • 6
  • 19
  • I don't see the `new` method in your controller where the `user_role` is defined for use in the view. What do the `params` look like in the server log when you submit the form? The error means that params doesn't have a `user_role` in the params hash and your `user_role_params` is requiring that it be there. So you need to figure out why the params hash isn't sending over the fields correctly. – trueinViso Feb 05 '17 at 06:39
  • Does `<%= form_for(@user_role) do |f| %>` work for you? Make sure you have `@user_role = UserRole.new` in your controller `new` method. – Zulhilmi Zainudin Feb 05 '17 at 08:36
  • `params.permit(:role_id,:user_id)` – wesley6j Feb 05 '17 at 13:56
  • Possible duplicate of [Param is missing or the value is empty: ParameterMissing in ResultsController#update](http://stackoverflow.com/questions/30825735/param-is-missing-or-the-value-is-empty-parametermissing-in-resultscontrollerup) – Sully Feb 05 '17 at 16:29
  • I already edit the post with all my controller code , if i chage the form to this: <%= form_for(@user_role) do |f| %> it does not work . If i change the params to : params.permit(:role_id,:user_id) it does not work. In the new method i am setting user_role : @user_role = UserRole.new – Andres Feb 05 '17 at 17:33

1 Answers1

1

You need to put an f before collection set like this:

<%= form_for(user_role) do |f| %>
 <% if user_role.errors.any? %>
  <div id="error_explanation">
   <h2><%= pluralize(user_role.errors.count, "error") %> prohibited this user_role from being saved:</h2>

   <ul>
    <% user_role.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
   </ul>
  </div>
 <% end %>
 <div class="field">
 <%= f.label :user %>
 <%= f.collection_select(:user_id, @users, :id, :name,{:selected => @selected_user}) %>
</div>

<div class="field">
 <%= f.label :rol %>
 <%= f.collection_select(:role_id, @roles, :id, :role {:selected => @selected_role}) %>
</div>

<div class="actions">
 <%= f.submit %>
</div>

otherwise the params are being sent like user_id instead of user_role[user_id]

  • I already change my form to :<%= form_for(@user_role) do |f| %> and i get the same error. – Andres Feb 05 '17 at 17:42
  • do i have to change my params too? because i change the form to f.collection_select and i get this error: undefined method `merge' for :name:Symbol Extracted source (around line #15):
    <%= f.label :user %> <%= f.collection_select(:user_id, :user_id,@users, :id, :name,{:selected => @selected_user}) %>
    – Andres Feb 05 '17 at 17:50
  • I meant to remove the first parameter of the collection set method because the form_for should now automatically supply it. Try it now. –  Feb 05 '17 at 18:03
  • Great, glad I was able to help –  Feb 05 '17 at 18:15