我是ruby on rails的新手,我试图更新我的用户,但是当我的用户更新时,我无法redirect_to我的current_user。这是我的控制者:
class WelcomeController < ApplicationController
def index
@users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
end
def create
user = User.login(params[:session][:username], params[:session][:pass])
if user
login user
redirect_to root_url
else
redirect_to welcome_index_path
end
end
def sucess
@user = User.find(params[:id])
@relation = Relation.new
end
def edit
if current_user.update_attributes(:image => params[:image])
logger.debug "image: #{current_user.image}"
if current_user.changed?
redirect_to current_user
else
render 'edit'
end
else
end
end
def destroy
logout
redirect_to root_url
end
end
我的路线
get "uploads/new"
get "micropost/new"
get "user/new"
get "user/saved"
get "post/new"
get "post/show"
get "welcome/index"
get "welcome/sucess"
get "welcome/edit"
get "welcome/test"
root :to => "welcome#index"
get '/users/:id', :to => 'welcome#sucess', :as => "user"
match '/relations', to: 'relation#create', via: 'post'
match '/relations/:id', to: 'relation#destroy', via: 'delete'
resources :users
resources :relations, only: [:create, :destroy]
resources :microposts
resources :uploads
match '/welcome/edit', to: 'welcome#edit', via: 'post'
match '/login', to: 'welcome#create', via: 'post'
match '/logout' => 'welcome#destroy', as: :logout
match '/create', to: 'micropost#create', via: 'post'
match '/signup', to: 'user#signup', via: 'post'
当我检查日志文件时:
Started POST "/welcome/edit?method=post" for 127.0.0.1 at 2014-11-13 15:09:00 +0700
Processing by WelcomeController#edit as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XHQeEBjnndipJ8c4TrOnn5Yzh7xqvcEhl4+L7BbGBY0=", "image"=>"Hydrangeas.jpg", "commit"=>"Save", "method"=>"post"}
[1m[35mUser Load (1.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mUser Exists (0.0ms)[0m SELECT 1 AS one FROM "users" WHERE ("users"."username" = 'sa1234' AND "users"."id" != 10) LIMIT 1
[1m[36m (1.0ms)[0m [1mUPDATE "users" SET "image" = 'Hydrangeas.jpg', "updated_at" = '2014-11-13 08:09:00.576402' WHERE "users"."id" = 10[0m
[1m[35m (157.0ms)[0m commit transaction
[1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
image: Hydrangeas.jpg
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
Rendered welcome/edit.html.erb within layouts/application (4.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
Completed 200 OK in 219.0ms (Views: 52.0ms | ActiveRecord: 159.0ms)
我不知道我的应用程序如何在没有我的current_user更新成功的情况下进行编辑,请!帮我解决这个问题:)
更新: @Jakob S,这是我尝试你的代码时的日志:
Started GET "/welcome/edit" for 127.0.0.1 at 2014-11-13 15:21:57 +0700
Processing by WelcomeController#edit as HTML
[1m[36mUser Load (1.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
[1m[35m (0.0ms)[0m begin transaction
[1m[36mUser Exists (1.0ms)[0m [1mSELECT 1 AS one FROM "users" WHERE ("users"."username" = 'sa1234' AND "users"."id" != 10) LIMIT 1[0m
[1m[35m (0.0ms)[0m commit transaction
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
image:
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
Redirected to http://localhost:3000/users/10
Completed 302 Found in 5.0ms (ActiveRecord: 2.0ms)
Started GET "/users/10" for 127.0.0.1 at 2014-11-13 15:21:57 +0700
Processing by WelcomeController#sucess as HTML
Parameters: {"id"=>"10"}
[1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", "10"]]
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" INNER JOIN "relations" ON "users"."id" = "relations"."following_id" WHERE "relations"."follower_id" = 10
[1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" INNER JOIN "relations" ON "users"."id" = "relations"."follower_id" WHERE "relations"."following_id" = 10[0m
Rendered welcome/sucess.html.erb within layouts/application (7.0ms)
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
[1m[36mCACHE (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m [["id", 10]]
[1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 10]]
Completed 200 OK in 39.0ms (Views: 37.0ms | ActiveRecord: 0.0ms)
答案 0 :(得分:0)
current_user.changed?
导致您的问题。
changed?
是Rails'Dirty model checking的一部分。保存模型时,将重置模型脏状态(即更改了哪些属性)。
update_attributes
updates all attributes and saves the object。因此current_user.changed?
在false
之后始终为update_attributes
,因为自上次保存以来模型尚未更改。
删除对changed?
及其相关render "edit"
部分的检查,您应该好好去:
def edit
if current_user.update_attributes(:image => params[:image])
redirect_to current_user
else
render 'edit'
end
end
额外提示:一般来说,您正在实施的更新资源的行为通常会采用update
行动。 edit
操作通常用于呈现用于更改资源的UI。