Ruby on Rails redirect_to 保存后不会重定向,只会保留在页面上(但会保存)
这是我的代码:
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)
保存后用户不会重定向到临时成功页面。我错过了什么吗? 我也尝试过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 技术交流群。
发布评论
评论(2)
花间憩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)并将您想要的任何内容放入其中
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
对控制器的这个简单更改解决了我的问题:
我将:替换
为:
现在一切正常。
决定暂时不去理会验证码
This simple change to the controller solved my issue:
I replaced:
with:
all is working fine now.
Decided to not bother with recaptcha for now