Ruby on rails redirect_to在保存后不会重定向,只停留在页面上(但保存)

时间:2011-10-26 23:01:41

标签: ruby-on-rails ruby ruby-on-rails-3 rubygems ruby-on-rails-3.1

这是我的代码:

class UsersController < ApplicationController

  def new
    @user = User.new
  end

   def create
      @user = User.new(params[:user])     
      respond_to do |format|
      if @user.save
      format.html { redirect_to :success }  

      else

      format.html { render :new }  
      format.js   { render :form_errors }
      end
    end
  end

end

查看:

<div id="joinFormContainer"> 
<%= form_for @user, :remote => true do |f| %>
      <div id="firstNameField">
       <%= f.text_field :first_name, :placeholder => "First Name" %>
      </div>
      <div id="lastNameField">
   <%= f.text_field :last_name, :placeholder => "Last Name" %> 
      </div>
      <div id="emailFieldJoin">
   <%= f.text_field :email, :placeholder => "Email"  %>
      </div>

      <div id="passwordFieldJoin">
   <%= f.password_field :password, :placeholder => "Password"  %>
      </div>
      <div id="usernameField">
   <%= f.text_field :username, :placeholder => "Username"  %>
      </div>

      <div id="joinButton">      <%= f.submit 'Join Us', :id =>"join_submit" %> </div> 
    </div><% end %>
   <div id="error_explanation">     

<%= @user.errors.full_messages.first if @user.errors.any? %>

 </div>
  </div>
  <p>&nbsp;</p>

  </div>
</div>

日志:

Binary data inserted for `string` type on column `encrypted_password`
Binary data inserted for `string` type on column `password_salt`
  SQL (0.9ms)  INSERT INTO "users" ("created_at", "email", "encrypted_password", "first_name", "last_name", "password_salt", "updated_at", "username") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Wed, 26 Oct 2011 23:40:03 UTC +00:00], ["email", "fdfdfdfdfdffdf@sdsd.sdd"], ["encrypted_password", "$2a$10$vFOoxHfvc3N2jNPMgx3iN.cNrxENvO5qAaSTaUa5itmzb0uADV9ZS"], ["first_name", "fddffdf"], ["last_name", "dffdffddff"], ["password_salt", "$2a$10$vFOoxHfvc3N2jNPMgx3iN."], ["updated_at", Wed, 26 Oct 2011 23:40:03 UTC +00:00], ["username", "fdgdgddgfdfd"]]
Redirected to http://localhost:3000/success
Completed 302 Found in 87ms


Started GET "/success" for 127.0.0.1 at 2011-10-27 00:40:03 +0100
  Processing by UsersController#success as JS
Rendered users/success.html.erb within layouts/application (0.0ms)
Completed 200 OK in 10ms (Views: 10.1ms | ActiveRecord: 0.0ms)

保存后,用户不会重定向到临时成功页面。我错过了什么吗? 我也试过redirect_to'成功'

2 个答案:

答案 0 :(得分:2)

对控制器的这一简单改动解决了我的问题:

我换了:

format.html { redirect_to :success } 

使用:

format.js { render :js => "window.location = '#{success_path}'" }

现在一切正常。

现在决定不打扰recaptcha

答案 1 :(得分:0)

编辑:

从阅读日志开始,您的表单将作为JS处理。如果您想要重定向,请立即从表单中删除remote =&gt; true

其余的是我原来的答案,无论如何都可以实现其中一些:


我认为你需要redirect_to success_path

更新:

好的,这会有用,(希望如此):

1-在routes.rb中执行以下操作:

resources :users do
  member do
    get 'success'
  end
end

在用户控制器中,创建一个名为success

的新操作
def success
  @user = User.find(params[:id])
end

在创建操作

def create
  @user = User.new(params[:user])     
  respond_to do |format|
  if @user.save
    format.html { redirect_to success_user_path(@user) }  
  else
    format.html { render :new }  
    format.js   { render :form_errors }
  end
end

然后创建/views/users/success.html.erb(或haml)并在其中放入任何内容