Andres Franco Andres Franco - 4 years ago 202
Ruby Question

Error in rails 5 form on Create

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.

Answer Source

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]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download