[英]can't redirect_to current_user in ruby on rails
I'm newbie in ruby on rails, and i trying to update my user, but when my user updated, i can't redirect_to my current_user. 我是ruby on Rails的新手,并且尝试更新我的用户,但是当我的用户更新时,我无法redirect_to我的current_user。 This is my controller:
这是我的控制器:
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
my routes 我的路线
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'
When i check log file: 当我检查日志文件时:
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)
I don't know how my app render edit without my current_user update sucessfull, please! 我不知道在没有current_user更新成功的情况下我的应用程序如何渲染编辑,请! help me to fix that :)
帮我解决这个问题:)
Update: @Jakob S, this is my log when i try your code: 更新:@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)
The current_user.changed?
current_user.changed?
is causing your problems. 造成您的问题。
changed?
is part of Rails' Dirty model checking . 是Rails的脏模型检查的一部分 。 A models dirty states (ie which attributes were changed) is reset when the model is saved.
保存模型后,将重置模型的脏状态(即更改了哪些属性)。
update_attributes
updates all attributes and saves the object . update_attributes
更新所有属性并保存对象 。 Thus current_user.changed?
因此
current_user.changed?
will always be false
right after update_attributes
, because the model hasn't been changed since the last save. 在
update_attributes
之后总是将为false
,因为自上次保存以来未更改模型。
Remove the check for changed?
删除检查是否已
changed?
and its related render "edit"
part, and you should be good to go: 及其相关的
render "edit"
部分,那么您应该很好:
def edit
if current_user.update_attributes(:image => params[:image])
redirect_to current_user
else
render 'edit'
end
end
Bonus tip: Generally speaking an action like the one you're implementing, where you update a resource, would usually go in the update
action. 温馨提示:一般来说,像正在实施的操作一样,在其中更新资源的操作通常会在
update
操作中进行。 The edit
action is usually used for rendering the UI for changing a resource. edit
动作通常用于呈现用于更改资源的UI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.