2013-11-21 35 views
0

我有麻煩保存與連接表相關的數據。我正在使用Rails 4.01 Devise 3.23和rolify。我對Rails相當陌生,所以這可能是一個小問題,儘管我已經搜索了一切,所以我可以。在連接表中不更新的表,我不知道爲什麼

基本上,當我嘗試更改角色時,它不會保存連接表中的數據,但它似乎不會引發任何錯誤。我在日誌中也找不到任何東西。 我有第二個連接表完全相同的問題。共同點是他們都加入到我的用戶表中,並在我的用戶控制器上使用更新操作。

我花了最後兩個小時試圖找到解決方案。我認爲這可能是一個強烈的參數問題,但我找不到問題。我曾嘗試降級到Rails 4.0.0,以防萬一,但不行。我有另一個基本相同的代碼,沒有問題的應用程序。

這裏是我的用戶控制器:

class UsersController < ApplicationController 
    before_filter :authenticate_user! 
    before_action :set_user, only: [:show, :edit, :update, :destroy] 

    def index 
    authorize! :index, @user, :message => 'Not authorized as an administrator.' 
    @users = User.all 
    end 

    def show 
    end 

    def update 
    authorize! :update, @user, :message => 'Not authorized as an administrator.' 
    if @user.update_attributes(user_params[:user]) 
     redirect_to users_path, :notice => "User updated." 
    else 
     redirect_to users_path, :alert => "Unable to update user." 
    end 
    end 

    def destroy 
    authorize! :destroy, @user, :message => 'Not authorized as an administrator.' 
    user = User.find(params[:id]) 
    unless user == current_user 
     user.destroy 
     redirect_to users_path, :notice => "User deleted." 
    else 
     redirect_to users_path, :notice => "Can't delete yourself." 
    end 
    end 

    # new function to set the password without knowing the current password used in our confirmation controller. 
    def attempt_set_password(params) 
    p = {} 
    p[:password] = params[:password] 
    p[:password_confirmation] = params[:password_confirmation] 
    update_attributes(p) 
    end 

    # new function to return whether a password has been set 
    def has_no_password? 
    self.encrypted_password.blank? 
end 

# new function to provide access to protected method unless_confirmed 
def only_if_unconfirmed 
    unless_confirmed {yield} 
end 

private 

    # Use callbacks to share common setup or constraints between actions. 
    def set_user 
     @user = User.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def user_params 
     params.require(:user).permit(:name, :role_ids, :qualification_id, :role, :qualification) 
    end 

end 

我的用戶模型:

class User < ActiveRecord::Base 
    belongs_to :qualification 

    rolify 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable, :confirmable 

    validates :phone, presence: true, format: { with: /1.\d{3}.\d{3}.\d{4}/, message: "Must be in 1.555.555.5555 format" } 
    validates :name, presence: true 
    validates :email, presence: true 
    validates :acknowledgement, presence: true 


# new function to set the password without knowing the current password used in our confirmation controller. 
def attempt_set_password(params) 
    p = {} 
    p[:password] = params[:password] 
    p[:password_confirmation] = params[:password_confirmation] 
    update_attributes(p) 
end 

# new function to return whether a password has been set 
def has_no_password? 
    self.encrypted_password.blank? 
end 

# new function to provide access to protected method unless_confirmed 
def only_if_unconfirmed 
    pending_any_confirmation {yield} 
end 

def password_required? 
    # Password is required if it is being set, but not for new records 
    if !persisted? 
    false 
    else 
    !password.nil? || !password_confirmation.nil? 
    end 
end 

end 

我的資格型號:

class Qualification < ActiveRecord::Base 
    has_many :users 
end 

我的榜樣:

class Role < ActiveRecord::Base 
    has_and_belongs_to_many :users, :join_table => :users_roles 
    belongs_to :resource, :polymorphic => true 

    scopify 
end 

這裏是我的索引視圖更改的角色:

<h2 class='subheader'>People</h2> 
    <% @users.each do |user| %> 
    <div class='row collapse'> 
     <div class='large-2 columns'><%= link_to user.name, user_path(user) %></div> 
     <div class='large-2 columns'><%= user.email %></div> 
     <div class='large-2 columns'><%= user.phone %></div> 
     <div class='large-2 columns'><%= user.qualification.name unless user.qualification.nil?%></div> 
     <div class='large-2 columns'><%= user.roles.first.name.titleize unless user.roles.first.nil? %></div> 
     <div class='large-2 columns'> 
     <% if user != current_user %> 
     <a data-reveal-id="role-options-<%= user.id %>" href="#" class="button tiny radius" type="button">Change role</a> 
     <% end %> 
     </div> 
     <div class='large-2 columns'> 
     <%= link_to("Delete Person", user_path(user), :data => { :confirm => "Are you sure?" }, method: :delete, class: 'button radius tiny') unless user == current_user %></div> 
     <hr> 
    </div> 
<div id="role-options-<%= user.id %>" class="reveal-modal tiny" style="display: none;"> 
    <%= simple_form_for user, url: user_path(user), html: {:method => :put, class: 'custom' } do |f| %> 
    <div> 
    <a class="close-reveal-modal">&#215;</a>  
    <h3>Change Role</h3> 
    </div> 
    <div> 
    <%= f.input :role_ids, collection: Role.all, as: :radio_buttons, label_method: lambda {|t| t.name.titleize}, label: false, item_wrapper_class: 'inline', checked: user.role_ids.first %> 
    </div> 
    <div> 
    <%= f.submit "Change Role", :class => "button tiny radius" %> 
    </div> 
    <% end %> 
</div> 
<% end %> 

下面是從我的日誌相關的摘錄:

Started PUT "https://stackoverflow.com/users/47" for 127.0.0.1 at 2013-11-20 22:27:59 -0700 
Processing by UsersController#update as HTML 
    Parameters: {"utf8"=>"✓",  "authenticity_token"=>"3kfL/K47ynC4LGF9kh/cAfAadGu8OXhH9kXhxeGPsvo=", "user"=> {"role_ids"=>"2"}, "commit"=>"Change Role", "id"=>"47"} 
    [1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 
    [1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", "47"]] 
    [1m[35m (0.1ms)[0m SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = ? AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] 
    [1m[36m (0.1ms)[0m [1mbegin transaction[0m 
    [1m[35m (0.1ms)[0m commit transaction 
Redirected to http://localhost:3000/users 
Completed 302 Found in 8ms (ActiveRecord: 0.7ms) 

我拉我的頭髮就這一個,所以任何幫助將是不勝感激。

+0

你得到了什麼錯誤? –

+0

我完全沒有錯誤。表簡單不會更新。 –

+0

'params'看起來像什麼被傳遞給'update'方法?這些將在日誌文件'log/development.log'中(假設開發)。 – CDub

回答

0

我不是超級熟悉rolify,但它並不像你User模型做任何事情與在PARAMS的role_ids屬性...

假如沒有的話,你會想要遍歷在每個role_id(如果有的話),你的控制器中:

def update 
    authorize! :update, @user, :message => 'Not authorized as an administrator.' 

    @user.update_roles(user_params[:role_ids]) if user_params[:role_ids].present? 

    if @user.update_attributes(user_params[:user]) 
    redirect_to users_path, :notice => "User updated." 
    else 
    redirect_to users_path, :alert => "Unable to update user." 
    end 
end 

然後在你的User模式,你必須:

def update_roles(role_ids) 
    role_names = Role.find_all_by_id(role_ids).map(&:name) 

    role_names.each {|role_name| self.add_role role_name.to_sym } 
end 

再一次,我沒有測試過這個,我也不太熟悉rolify,所以別人可能有更好的建議,但這至少應該讓你更接近。

+0

我試了一下,但我得到了和以前一樣的結果。Rolify必須已經這樣做。 –

相關問題