簡體   English   中英

無法在Rails上的ruby中重定向到current_user

[英]can't redirect_to current_user in ruby on rails

我是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)

current_user.changed? 造成您的問題。

changed? 是Rails的臟模型檢查的一部分 保存模型后,將重置模型的臟狀態(即更改了哪些屬性)。

update_attributes 更新所有屬性並保存對象 因此current_user.changed? update_attributes之后總是將為false ,因為自上次保存以來未更改模型。

刪除檢查是否已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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM