【发布时间】:2015-03-13 11:04:56
【问题描述】:
我一直在审查使重定向正常工作的所有解决方案。我重写了注册控制器并且重定向工作,但是我的不足之处是获取简单的表单来处理错误。如果我通过标准路由到设计视图,它会显示正确的错误和 css,但是,当我在重定向到我的 root_path(pages#index) 路由时这样做时,我什么也看不到,并且表单没有改变.
表格代码:
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-group top">
<%= f.input :name, required: true, placeholder: "George Washington" %>
</div>
<div class="form-group">
<%= f.input :email, required: true, placeholder: "Your.email.address@email.com" %>
</div>
<div class="form-group">
<%= f.input :password, required: true, hint: "7 characters minimum. No emojis allowed." %>
</div>
<div class="form-group">
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign me up and get me my key", class: "btn btn-wide thirdy" %>
<small class="note">By submitting your information you are agreeing to our Terms & Conditions.</small>
</div>
<% end %>
routes.rb:
Rails.application.routes.draw do
devise_for :users, controllers: {
registrations: "registrations"
}
get 'pages/index'
get 'docs', to: 'pages#docs', as: 'docs'
root to: 'pages#index'
end
registrations_controller.rb
def create
build_resource(sign_up_params)
resource.save
yield resource if block_given?
if resource.persisted?
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
redirect_to root_path
end
end
pages_controller.rb
class PagesController < ApplicationController
def index
end
def docs
end
end
【问题讨论】:
-
你能修复它吗?
-
我做到了,再次感谢您的跟进。我遇到的问题是资源范围配置不正确。我将配置添加到 application_helper.rb,然后覆盖注册控制器的“创建”方法。出错时,我会“渲染”而不是“redirect_to”,它会使用我需要的错误消息破坏用户对象。我将编辑我的帖子以更好地说明我的解决方案。再次感谢您的帮助。
标签: ruby-on-rails ruby redirect devise ruby-on-rails-4.2