2012-01-19 99 views
0

我是Ruby on Rails的新手。我有一個具有這些屬性的用戶模型:登錄,電子郵件,角色和組。更新記錄 - Ruby On Rails 3.1

這裏我的文件/app/models/user.rb

# == Schema Information 
# 
# Table name: users 
# 
# id   :integer   not null, primary key 
# login  :string(255) 
# email  :string(255) 
# role  :integer 
# group  :integer 
# created_at :datetime 
# updated_at :datetime 
# 

class User < ActiveRecord::Base 
    attr_accessible :login, :email, :role, :group 

     def self.search(search) 
     if search 
      where('login LIKE ?', "%#{search}%") 
      else 
      scoped 
     end 
    end 
end 

這裏我的應用程序/視圖/用戶/ edit.html.erb

<%= form_for(@user) do |f| %> 
    <div class="field"> 
    <%= f.label :login %><br /> 
    <%= f.text_field :login, :readonly => true%> 
    </br> 
    <%= f.collection_select(:role, Role.all, :id, :name) 
%> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Update" %> 
    </div> 
<% end %> 

當我點擊更新按鈕,我轉到用戶顯示頁面。 我這有跡:

Started PUT "https://stackoverflow.com/users/4" 
    processing by UsersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"HBB9IcKpWyjSbrmh6os=", "user"=>{"role"=>"3"}, "commit"=>"Update", "id"=>"4"} 
Redirected to http%3A%2F%2F10.100.2.66%3A3000%2Fusers%2F4 
Completed 302 Found in 1ms 

這裏我的應用程序/控制器/ users_controller.rb

class UsersController < ApplicationController 
    before_filter :require_admin 
    helper_method :sort_column, :sort_direction 


    def new 
    @title = "Sign up" 
    end 

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

    end 

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

    def index 
    @users = User.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:page => params[:page],:per_page => 10) 

    end 

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

    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated." 
     redirect_to @user 
    else 
     @title = "Edit user" 
     render 'edit' 
    end 

    end 

    def get 
    @user = User.find(params[:login]) 
    end 

我不明白爲什麼這是行不通的。如果在索引中,我強制更新這樣的用戶,它正在工作:

def index 
    @testUser = User.find(6) 
    @test.user.update_attributes(:role => "2") 
    ... 
end 

這就像更新中的指令沒有執行。如果我這樣做:

def update 
    logger.info("i am in update) 
    ... 
end 

我沒有任何日誌

這裏耙路線命令的結果:

我希望我是清楚的。對不起,我的英語很糟糕。

感謝您的幫助。

+0

什麼是你的問題是什麼呢? –

+0

我的問題是我無法更新我的用戶記錄。 –

回答

0

日誌表明,它確實達到了更新操作:

Started PUT "https://stackoverflow.com/users/4" 
    processing by UsersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"HBB9IcKpWyjSbrmh6os=", "user"=>{"role"=>"3"}, "commit"=>"Update", "id"=>"4"} 
Redirected to http://10.100.2.66:3000/users/4 
Completed 302 Found in 1ms 

的第一行看起來像表演動作,但要記住展現行動是GET請求。作爲「PUT」請求意味着它被路由到更新操作。運行rake routes證實了這一點:

rake routes 
PUT /users/:id(.:format) {:controller=>"users", :action=>"update"} 

這進一步線2證實:processing by UsersController#update as HTML告訴我們Rails是處理UsersController的更新動作的HTML(而不是JSON爲例)。

關於另一個話題,我注意到你的User#role屬性是誤導性的。你應該將其命名爲role_id以表明它與Role相關的整數,並更新你這樣的代碼:

# user.rb 
attr_accessible :role_id 
belongs_to :role 

# app/views/users/edit.html.erb 
<%= f.collection_select(:role_id, Role.all, :id, :name) 

祝你好運!

+0

你好,謝謝你的回答和你的幫助。我已將User#角色屬性更改爲User#role_id。但對於主題來說,對於你來說,一切似乎都好?我必須發佈更多細節嗎? –

0

也許是因爲錯誤的變量:

def index 
    @testUser = User.find(6) 
    @test.user.update_attributes(:role => "2") 
    ... 
end 

它必須是:

def index 
    @testUser = User.find(6) 
    @testUser.update_attributes(:role => "2") 
    ... 
end 

或者,也許你可以在你這一點:

@testUser = User.find(6) 
@testUser.role = 2 
@testUser.save