分配角色未插入其相应的模型

时间:2019-01-24 09:03:04

标签: ruby-on-rails ruby-on-rails-3.2

我正在实现向用户分配角色。我已经使用很多关联来实现了这一点。要求说,特定用户可以有任意多个角色。已经创建了用户,角色模型,并将这两个与用户角色模型链接。当我将角色分配给特定用户时,它没有保存在我的用户角色表中。和我的代码是屁股 //用户模型的index.html.erb

我使用以下属于User模型的index.html.erb代码调用分配角色页面,我在userrole模型的new.html.erb URL中获取用户ID,因此未获取用户ID是不是我的问题。

<pre>
<%= link_to t('.assign_role', default: t("helpers.links.assign")),
                  new_user_role_path(user_id: user.id,user_name: user.first_name),
                 method: :get,
                 class: 'btn btn-default btn-xs' %>
</pre>

// new.html.erb

<h1>Assign Roles to <%=params[:user_name ]%></h1>
<%= form_for @user_role, :html => { :class => "form-horizontal user" } do |f| %>

    <div class="form-group">
        <%= f.label :Assign_Role_1, :class => 'control-label col-lg-2' %>
        <div class="col-lg-10" >
            <%= f.select(:role_id, Role.all.map{|e| [e.name, e.id]},{ :include_blank => "Please select" }, { class: 'form-control' } ) %>
        </div>
        <%=f.error_span(:Assign_Role) %>
    </div>


    <div class="form-group">
      <div class="col-lg-offset-2 col-lg-10">
        <%= f.submit nil, :class => 'btn btn-primary' %>
        <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                  users_path, :class => 'btn btn-default' %>
      </div>
    </div>

  <% end %>

//控制器

   class UserRolesController < ApplicationController   

    def index
    @user_role = UserRole.all   
    end

    def new
    @user_role = UserRole.new   
    end

     def create  
      @user_role = UserRole.new(user_role_params)
        if UserRole.exists?(user_id: user_role_params[:user_id]) 
              flash[:error] = "User already exists."
              redirect_to '/' and return
        end
        if @user_role.save
              flash[:success] = "New UserRole created."
              redirect_to @user_role
        else
              render 'new'
        end
     end

     def edit   
     end

     def destroy  
     end

     def show
       @user_role = UserRole.all   
     end


  private  
     def user_role_params
        params.require(:user_role).permit( :role_id,:user_id
                                 )   
     end

结束

我的桌子

create_table "user_roles", force: :cascade do |t|
    t.integer "role_id"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["role_id"], name: "index_user_roles_on_role_id"
    t.index ["user_id"], name: "index_user_roles_on_user_id"
  end

由于我是红宝石的婴儿,刚出生时就处于危险之中,因此即使错误,也请指导我。

2 个答案:

答案 0 :(得分:0)

根据您的评论,我认为您没有将user_id传递给控制器​​。尝试如下操作:

def new
  # Get the user somehow
  user = User.find_by(name: params[:user_name])
  # associate the new role with the user
  @user_role = user.user_roles.new   
end

def create
  # Get the user somehow
  user = User.find_by(name: params[:user_name])

  # associate the new role with the user
  @user_role = user.user_roles.new(user_role_params)

  if @user_role.save
    flash[:success] = "New UserRole created."
    redirect_to @user_role
  else
    render 'new'
  end
end

您可能需要尝试一下,因为您的设置有点不寻常。但是你会到达的:)。祝你好运。

答案 1 :(得分:0)

如果您在表单中获取到user_id,请尝试以下表单中的代码

from wagtail.contrib.modeladmin.options import ModelAdmin
from myviews import FamilyTreeCreateView

class FamilyTreeAdmin(ModelAdmin):
    create_view_class = FamilyTreeCreateView

在您的控制器中

<h1>Assign Roles to <%=params[:user_name ]%></h1>
<%= form_for @user_role, :html => { :class => "form-horizontal user" } do |f| %>
    <%= f.hidden_field :user_id, value: @user_id %>
    <div class="form-group">
        <%= f.label :Assign_Role_1, :class => 'control-label col-lg-2' %>
        <div class="col-lg-10" >
            <%= f.select(:role_id, Role.all.map{|e| [e.name, e.id]},{ :include_blank => "Please select" }, { class: 'form-control' } ) %>
        </div>
        <%=f.error_span(:Assign_Role) %>
    </div>


    <div class="form-group">
      <div class="col-lg-offset-2 col-lg-10">
        <%= f.submit nil, :class => 'btn btn-primary' %>
        <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                  users_path, :class => 'btn btn-default' %>
      </div>
    </div>

  <% end %>

代替检查角色,而是在用户角色模型中添加验证,如下所示:

def new
  @user_id = params[:user_id]
  @user_role = UserRole.new   
end

def create  
  @user_role = UserRole.new(user_role_params)
    if UserRole.exists?(user_id: user_role_params[:user_id]) 
          flash[:error] = "User already exists."
          redirect_to '/' and return
    end
    if @user_role.save
          flash[:success] = "New UserRole created."
          redirect_to @user_role
    else
          render 'new'
    end
end

private  
 def user_role_params
    params.require(:user_role).permit( :role_id,:user_id
                             )   
 end

或者,如果可能,不要在选择框中显示该用户已经存在的角色