Rails Tutorial dot org redirect_to保存但不显示

时间:2014-09-22 23:37:15

标签: ruby-on-rails

我正在通过railstutorial.org工作并在第7章。当我添加新用户时,控制器将用户保存到数据库然后挂起语句redirect_to @user然后大约2分钟后返回url /用户声称用户已存在。看起来它正在做的是成功执行创建控制器,保存用户,挂起redirect_to调用,然后调用带有错误的创建操作。这是我的代码,提前感谢您的任何帮助

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      #flash[:success] = "Welcome to the Sample App!"
      #render 'show'
      redirect_to user_url(@user.id)
    else
      render 'new'
    end
  end

  private
    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end
end 

和routes.rb

SampleApp::Application.routes.draw do

  match '/signup',  to: 'users#new',              via: 'get'
  resources :users
  root 'static_pages#home'
  #match '/create',  to: 'user#show',              via: 'post'

  match '/help',    to: 'static_pages#help',      via: 'get'
  match '/about',   to: 'static_pages#about',     via: 'get'
  match '/contact', to: 'static_pages#contact',   via: 'get'

编辑: 所以我粘贴了下面的开发日志的输出。我想好消息是它确实首先产生了一个合适的重定向(重定向Redirected to https://rails_chp3-c9-jd8001.c9.io/users/26是我期望它做的......所以这很好)。不幸的是,页面永远不会呈现,并在两分钟后用POST数据重新加载/ user url。我还删除了状态:see_other并返回(上面更新的当前代码)

Started POST "/users" for 64.125.248.3 at 2014-09-23 01:36:02 +0000
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IXfkamhDHYRBHQPdMfVyp7/0zXyTONWCMfk9vO+ljHY=", "user"=>{"name"=>"aus8", "email"=>"aus8@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
  [1m[35m (0.2ms)[0m  begin transaction
  [1m[36mUser Exists (0.3ms)[0m  [1mSELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('aus8@gmail.com') LIMIT 1[0m
Binary data inserted for `string` type on column `password_digest`
  [1m[35mSQL (15.3ms)[0m  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Tue, 23 Sep 2014 01:36:03 UTC +00:00], ["email", "aus8@gmail.com"], ["name", "aus8"], ["password_digest", "$2a$10$yYOz6cxU.mqjGlhl7Q743O0kOJJX930RmFaA1rsHjGCqz71HBIUi6"], ["updated_at", Tue, 23 Sep 2014 01:36:03 UTC +00:00]]
  [1m[36m (51.8ms)[0m  [1mcommit transaction[0m
Redirected to https://rails_chp3-c9-jd8001.c9.io/users/26
Completed 302 Found in 148ms (ActiveRecord: 67.7ms)


Started POST "/users" for 64.125.248.3 at 2014-09-23 01:38:03 +0000
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IXfkamhDHYRBHQPdMfVyp7/0zXyTONWCMfk9vO+ljHY=", "user"=>{"name"=>"aus8", "email"=>"aus8@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36mUser Exists (0.3ms)[0m  [1mSELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('aus8@gmail.com') LIMIT 1[0m
  [1m[35m (0.1ms)[0m  rollback transaction
  Rendered shared/_error_messages.html.erb (0.6ms)
  Rendered users/new.html.erb within layouts/application (5.0ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  Rendered layouts/_header.html.erb (0.4ms)
  Rendered layouts/_footer.html.erb (0.2ms)
Completed 200 OK in 100ms (Views: 25.2ms | ActiveRecord: 0.5ms)

2 个答案:

答案 0 :(得分:0)

我首先要删除

status: :see_other and return
来自重定向呼叫的

。此外,尝试查看您的日志,看看它是否给您任何想法。

答案 1 :(得分:0)

尝试更改您的create方法以重定向到@user而不是user_url(@ user.id) @ user.id,很可能是整数用户号。

def create
    @user = User.new(user_params)
    if @user.save
      #flash[:success] = "Welcome to the Sample App!"
      #render 'show'
      redirect_to @user
    else
      render 'new'
    end
  end