【问题标题】:Updated checkbox, when there is no data passed. ROR. What is going on?更新复选框,当没有数据通过时。罗尔。到底是怎么回事?
【发布时间】:2010-01-08 02:28:37
【问题描述】:

我在用户和角色之间构建了多对多关联。

当非管理员登录时,我隐藏了编辑角色功能,使用

使用以下代码:

查看/用户/edit.html.erb

<%= error_messages_for :user %>

<% form_for @user do |f| -%>
  <p><label for="login">Login</label><br/>
  <%= f.text_field :login %></p>

  <p><label for="email">Email</label><br/>
  <%= f.text_field :email %></p>

  <p><label for="password">Password</label><br/>
  <%= f.password_field :password %></p>

  <p><label for="password_confirmation">Confirm Password</label><br/>
  <%= f.password_field :password_confirmation %></p>

  <% if admin? %>
  <% for role in Role.find(:all) %>
 <div>
  <%= check_box_tag "user[role_ids][]", role.id, @user.roles.include?(role) %>
  <%= role.name %>
 </div>
 <% end %>
 <% else %>
 <% hidden_field :email, :email %>
 <% end %>

  <p><%= submit_tag 'Update' %></p>
<% end %>

终端输出,如我所料,如下:

Processing UsersController#update (for 127.0.0.1 at 2010-01-05 21:28:54) [PUT]
  Parameters: {"commit"=>"Update", "action"=>"update", "_method"=>"put", "authenticity_token"=>"E6qUNM3gS9OmxuAZpmF7FE2Mr/lowznNLMd6ENNT6uk=", "id"=>"5", "controller"=>"users", "user"=>{"password_confirmation"=>"[FILTERED]", "password"=>"[FILTERED]", "login"=>"lesa", "email"=>"lesa@gmail.com"}}

然而,当我感到惊讶时,用户失去了她的权利(在她的密码更新她的角色之前:[1,2])

>> user = User.find_by_login("lesa")
=> #<User id: 5, login: "lesa", email: "lesa@gmail.com", crypted_password: "58026ae120d0686196df3c72c9e3df5da596326d", salt: "f02ef9e00d16f1b9f82dfcc488fdf96bf5aab4a8", created_at: "2009-12-29 15:15:51", updated_at: "2010-01-05 21:28:54", remember_token: nil, remember_token_expires_at: nil>
>> user.role_ids
=> []
>> 

在我正在使用的用户模型控制器中:

attr_accessible :login, :email, :password, :password_confirmation, :role_ids

没有适当权限(角色)的用户怎么可能不更新他们的 role_id?显然,我所拥有的存在严重缺陷。下面是管理员更正 Lesa 的权限(角色)的终端输出。

Processing UsersController#update (for 127.0.0.1 at 2010-01-05 21:34:01) [PUT]
  Parameters: {"commit"=>"Update", "action"=>"update", "_method"=>"put", "authenticity_token"=>"/yC1s9T8yWZH+eM5fnhPwdeHPCTcT1d8IGoIn+tEd4Q=", "id"=>"5", "controller"=>"users", "user"=>{"password_confirmation"=>"[FILTERED]", "role_ids"=>["2"], "password"=>"[FILTERED]", "login"=>"lesa", "email"=>"lesa@gmail.com"}}

用户控制器代码:

class UsersController < ApplicationController
  # Be sure to include AuthenticationSystem in Application Controller instead

    #routine that is excecuted before every action in controller "Before_filter
  before_filter :login_required
  require_role "admin", :for => [:index, :create, :destroy]

  def index
    @users = User.find(:all)
  end

 def show
  @user = User.find(params[:id])
 end

 def destroy
  @user = User.find(params[:id])
  @user.destroy

  redirect_to(users_url)
 end

 def edit
    @user = User.find(params[:id])
 end

 def update
    @user = User.find(params[:id])

  params[:user][:role_ids] ||= []

  if @user.update_attributes(params[:user])
     flash[:notice] = 'User was successfully updated.'
    redirect_to(user_path(@user))
 else
    render :action => 'edit'
 end
 end

  # render new.rhtml
  def new
  end

  def create
    cookies.delete :auth_token
    # protects against session fixation attacks, wreaks havoc with 
    # request forgery protection.
    # uncomment at your own risk
    # reset_session
    @user = User.new(params[:user])
    @user.save
    if @user.errors.empty?
      self.current_user = @user
      redirect_back_or_default('/')
      flash[:notice] = "Thanks for signing up!"
    else
      render :action => 'new'
    end
  end

end

【问题讨论】:

    标签: ruby-on-rails checkbox many-to-many


    【解决方案1】:

    模型中有回调吗?你确定这些角色在你更新之前就已经存在了吗?你试过在用户模型中使用accepts_nested_attributes_for :roles 吗?我也很想看看你的控制器代码。

    更新 如果什么都没有出现,您的 User#update 操作会将其设置为一个空数组.... 不是理想的家伙,因为只有管理员会更新它们,并且所有其他更新请求都被设置为,好吧,正如您所发现的那样,什么都没有。报废它。对角色使用accepts_nested_attributes_并以某种方式锁定谁可以更新它们...

    【讨论】:

    • 是的,更新前肯定存在。我在上面添加了控制器代码。而且我还没有尝试过 accept_nested_attributes_for :roles。
    • 刚刚阅读了accepts_nested_attributes_for,我认为这不是我需要的。我的代码更新了角色,当我不希望它更新角色时它就无法运行。
    • 你读过我更新的部分了吗?如果它有效,我想你已经得到了答案。
    【解决方案2】:

    当然!

    我从控制器中删除了此代码:params[:user][:role_ids] ||= [] 以解决此问题。

    【讨论】:

      猜你喜欢
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多