[英]Update Record - 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
這是我的app / views / users / 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 "/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
這是我的app / controllers / 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
我沒有任何日志
這是rake route命令的結果:
我希望我很清楚。 對不起,我的英語,他很不好。
謝謝你的幫助。
也許是因為變量錯誤:
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
日志表明它確實正在執行更新操作:
Started PUT "/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
第一行看起來像show動作,但是請記住show動作是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相反), processing by UsersController#update as HTML
處理。
在另一個主題上,我注意到您的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)
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.