我是rails的新手,我试图创建一个表单来将角色与用户关联,我在表单中有两个collection_select,但是当我想创建或更新时,我收到此错误:
我的控制器代码是:
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
端
我的表单代码是:
<%= 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 %>
我做错了什么? ,在这种情况下我如何设置user_role_params? 我使用的是ruby 2.3.1和Rails:5.0.1
感谢您的建议。
答案 0 :(得分:1)
您需要在收集集之前放置一个f:
<%= 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>
否则,params会像user_id
而不是user_role[user_id]