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

发布于 12-12 06:19 字数 2754 浏览 1 评论 0原文

这是我的代码:

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

View:

<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>

log:

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", "[email protected]"], ["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“成功”

Here is my code:

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

View:

<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> </p>

  </div>
</div>

log:

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", "[email protected]"], ["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)

After save the user isn't redirected to temporary success page. Am I missing something?
I also tried redirect_to 'success'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぇ气2024-12-19 06:19:23

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

我将:替换

format.html { redirect_to :success } 

为:

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

现在一切正常。

决定暂时不去理会验证码

This simple change to the controller solved my issue:

I replaced:

format.html { redirect_to :success } 

with:

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

all is working fine now.

Decided to not bother with recaptcha for now

花间憩2024-12-19 06:19:23

编辑:

通过阅读您的日志,您的表单正在作为 JS 进行处理。如果您想要重定向,请暂时从表单中删除remote=>true

其余的是我原来的答案,无论如何实现其中一些可能是一个想法:


我认为您需要redirect_to success_path

更新:

好的,这会起作用,(希望):

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

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

在用户控制器中,创建一个名为成功的新操作

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)并将您想要的任何内容放入其中

EDIT:

From reading your logs, your form is being processed as JS. If you want the redirect, then remove the remote=>true from your form for now

The rest is my original answer, it may be an idea to implement some of it anyway:


I think you need redirect_to success_path

UPDATE:

ok, this will work, (hopefully):

1- in routes.rb do the following:

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

In User controller, create a new action called success

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

In Create action

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

Then create /views/users/success.html.erb (or haml) and put whatever you want in it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文