Rails:资源和表格
前言:我不确定资源是什么。
我需要此表单(通过 user/sign_up
路由运行)来在我的“离线页面”上工作--这样当我的应用程序关闭时用户仍然可以注册。我的 application_controller 调用 :filter_before, : except =>; [:offline]
和我的registrations_controller 有一个 :skip_filter_before
操作。 (这叫做动作吗?)
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
我的Registration_controller:
class RegistrationsController < Devise::RegistrationsController
before_filter :get_teams
skip_filter :require_online
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
respond_with resource, :location => redirect_location(resource_name, resource)
else
set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
respond_with_navigational(resource) { render_with_scope :new }
end
end
...
end
路由:
confirm_account /confirm_account(.:format) {:controller=>"confirmations", :action=>"confirm_account"}
sign_up /sign_up(.:format) {:action=>"sign_up", :controller=>"user/sign_up"}
new_user_session GET /user/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /user/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /user/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /user/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /user/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /user/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /user/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /user/cancel(.:format) {:action=>"cancel", :controller=>"registrations"}
user_registration POST /user(.:format) {:action=>"create", :controller=>"registrations"}
new_user_registration GET /user/sign_up(.:format) {:action=>"new", :controller=>"registrations"}
edit_user_registration GET /user/edit(.:format) {:action=>"edit", :controller=>"registrations"}
PUT /user(.:format) {:action=>"update", :controller=>"registrations"}
DELETE /user(.:format) {:action=>"destroy", :controller=>"registrations"}
user_confirmation POST /user/confirmation(.:format) {:action=>"create", :controller=>"confirmations"}
new_user_confirmation GET /user/confirmation/new(.:format) {:action=>"new", :controller=>"confirmations"}
GET /user/confirmation(.:format) {:action=>"show", :controller=>"confirmations"}
user_unlock POST /user/unlock(.:format) {:action=>"create", :controller=>"devise/unlocks"}
new_user_unlock GET /user/unlock/new(.:format) {:action=>"new", :controller=>"devise/unlocks"}
GET /user/unlock(.:format) {:action=>"show", :controller=>"devise/unlocks"}
editreject_admin GET /admin/:id/editreject(.:format) {:action=>"editreject", :controller=>"admin"}
reject_admin GET /admin/:id/reject(.:format) {:action=>"reject", :controller=>"admin"}
accept_admin GET /admin/:id/accept(.:format) {:action=>"accept", :controller=>"admin"}
entries_admin_index GET /admin/entries(.:format) {:action=>"entries", :controller=>"admin"}
preferences_admin_index GET /admin/preferences(.:format) {:action=>"preferences", :controller=>"admin"}
admin_index GET /admin(.:format) {:action=>"index", :controller=>"admin"}
about_entries GET /entries/about(.:format) {:action=>"about", :controller=>"entries"}
all_entries GET /entries/all(.:format) {:action=>"all", :controller=>"entries"}
myentries_entries GET /entries/myentries(.:format) {:action=>"myentries", :controller=>"entries"}
rate_entry GET /entries/:id/rate(.:format) {:action=>"rate", :controller=>"entries"}
submit_entry PUT /entries/:id/submit(.:format) {:action=>"submit", :controller=>"entries"}
entry_comments POST /entries/:entry_id/comments(.:format) {:action=>"create", :controller=>"comments"}
entry_comment DELETE /entries/:entry_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
entries GET /entries(.:format) {:action=>"index", :controller=>"entries"}
POST /entries(.:format) {:action=>"create", :controller=>"entries"}
new_entry GET /entries/new(.:format) {:action=>"new", :controller=>"entries"}
edit_entry GET /entries/:id/edit(.:format) {:action=>"edit", :controller=>"entries"}
entry GET /entries/:id(.:format) {:action=>"show", :controller=>"entries"}
PUT /entries/:id(.:format) {:action=>"update", :controller=>"entries"}
DELETE /entries/:id(.:format) {:action=>"destroy", :controller=>"entries"}
/auth/:service/callback(.:format) {:controller=>"services", :action=>"create"}
services GET /services(.:format) {:action=>"index", :controller=>"services"}
POST /services(.:format) {:action=>"create", :controller=>"services"}
root /(.:format) {:controller=>"entries", :action=>"index"}
countdown /countdown(.:format) {:controller=>"application", :action=>"countdown"}
Preface: I'm not sure what resources are.
I need this form (which is working from the user/sign_up
route) to work on my 'offline page'--so users can still register when my app is down. My application_controller calls a :filter_before, :except => [:offline]
and my registrations_controller has a :skip_filter_before
action. (Is it called an action?)
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :email %><br />
<%= f.email_field :email %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
My Registration_controller:
class RegistrationsController < Devise::RegistrationsController
before_filter :get_teams
skip_filter :require_online
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
respond_with resource, :location => redirect_location(resource_name, resource)
else
set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
respond_with_navigational(resource) { render_with_scope :new }
end
end
...
end
Routes:
confirm_account /confirm_account(.:format) {:controller=>"confirmations", :action=>"confirm_account"}
sign_up /sign_up(.:format) {:action=>"sign_up", :controller=>"user/sign_up"}
new_user_session GET /user/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /user/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /user/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /user/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /user/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /user/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /user/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /user/cancel(.:format) {:action=>"cancel", :controller=>"registrations"}
user_registration POST /user(.:format) {:action=>"create", :controller=>"registrations"}
new_user_registration GET /user/sign_up(.:format) {:action=>"new", :controller=>"registrations"}
edit_user_registration GET /user/edit(.:format) {:action=>"edit", :controller=>"registrations"}
PUT /user(.:format) {:action=>"update", :controller=>"registrations"}
DELETE /user(.:format) {:action=>"destroy", :controller=>"registrations"}
user_confirmation POST /user/confirmation(.:format) {:action=>"create", :controller=>"confirmations"}
new_user_confirmation GET /user/confirmation/new(.:format) {:action=>"new", :controller=>"confirmations"}
GET /user/confirmation(.:format) {:action=>"show", :controller=>"confirmations"}
user_unlock POST /user/unlock(.:format) {:action=>"create", :controller=>"devise/unlocks"}
new_user_unlock GET /user/unlock/new(.:format) {:action=>"new", :controller=>"devise/unlocks"}
GET /user/unlock(.:format) {:action=>"show", :controller=>"devise/unlocks"}
editreject_admin GET /admin/:id/editreject(.:format) {:action=>"editreject", :controller=>"admin"}
reject_admin GET /admin/:id/reject(.:format) {:action=>"reject", :controller=>"admin"}
accept_admin GET /admin/:id/accept(.:format) {:action=>"accept", :controller=>"admin"}
entries_admin_index GET /admin/entries(.:format) {:action=>"entries", :controller=>"admin"}
preferences_admin_index GET /admin/preferences(.:format) {:action=>"preferences", :controller=>"admin"}
admin_index GET /admin(.:format) {:action=>"index", :controller=>"admin"}
about_entries GET /entries/about(.:format) {:action=>"about", :controller=>"entries"}
all_entries GET /entries/all(.:format) {:action=>"all", :controller=>"entries"}
myentries_entries GET /entries/myentries(.:format) {:action=>"myentries", :controller=>"entries"}
rate_entry GET /entries/:id/rate(.:format) {:action=>"rate", :controller=>"entries"}
submit_entry PUT /entries/:id/submit(.:format) {:action=>"submit", :controller=>"entries"}
entry_comments POST /entries/:entry_id/comments(.:format) {:action=>"create", :controller=>"comments"}
entry_comment DELETE /entries/:entry_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
entries GET /entries(.:format) {:action=>"index", :controller=>"entries"}
POST /entries(.:format) {:action=>"create", :controller=>"entries"}
new_entry GET /entries/new(.:format) {:action=>"new", :controller=>"entries"}
edit_entry GET /entries/:id/edit(.:format) {:action=>"edit", :controller=>"entries"}
entry GET /entries/:id(.:format) {:action=>"show", :controller=>"entries"}
PUT /entries/:id(.:format) {:action=>"update", :controller=>"entries"}
DELETE /entries/:id(.:format) {:action=>"destroy", :controller=>"entries"}
/auth/:service/callback(.:format) {:controller=>"services", :action=>"create"}
services GET /services(.:format) {:action=>"index", :controller=>"services"}
POST /services(.:format) {:action=>"create", :controller=>"services"}
root /(.:format) {:controller=>"entries", :action=>"index"}
countdown /countdown(.:format) {:controller=>"application", :action=>"countdown"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Devise 中的“资源”是实际注册的资源。就您而言,它很可能是用户。但是,用户并未硬编码到 Devise 中,因此您可以拥有多种类型的用户,例如管理员或编辑者。在这种情况下,为了简单起见,当您阅读“资源”时,请考虑“用户”。
我的其余答案并不是真正的答案,只是要求提供更多详细信息。如果我能提供帮助,我会用正确的答案来编辑此内容:)
至于你的问题,我不能 100% 确定我明白你在问什么。您是否尝试在离线页面上显示注册表单或尝试将注册数据发送到离线页面?在这两种情况下,您是否收到错误或类似的情况阻止您这样做?如果是,请发布错误或意外行为的详细信息。发布您的routes.rb 也可能会有所帮助,具体取决于您的问题是什么。
还有一件事我不清楚,离线页面是您的 Rails 应用程序的一部分还是托管在其他地方?如果该应用程序已关闭,则将无法访问。
更新:
据我了解,您正在尝试将用户注册表单放在离线页面上?如果是这样,请尝试这个。
在您的控制器中:
在您的offline.html.erb 视图中:
我认为您没有为离线操作设置路线,因此您需要这样做。为了快速简单的方法,请使用如下内容:
其中
welcome
是离线操作所在的控制器的名称。这有帮助吗?
The 'resource' in the case of Devise is what is actually being registered. In your case, it's most likely a User. However, User isn't hardcoded into Devise so that you can have multiple types of users, for example Admins or Editors. For the sake of simplicity in this case, when you read 'resource' think 'user'.
The rest of my answer isn't really an answer, just a request for more details. I'll edit this with a proper answer if I'm able to help :)
As for your question, I'm not 100% sure I understand what it is you're asking about. Are you trying to show the registration form on an offline page or trying to send the registration data to the offline page? In either case, are you receiving and error or such like preventing you from doing so? If you are, please post the error or details of the unexpected behaviour. Posting your routes.rb might also be helpful, depending on what your problem is.
Just one other thing I'm not clear on, is the offline page part of your rails app or hosted elsewhere? If the app is down, it won't be accessible.
Update:
So what I understand is you're trying to put the user registration form on an offline page? If so, try this.
In your controller:
In your offline.html.erb view:
I don't think you have a route set up for your offline action, so you'll need to do that. For a quick and easy way, use something like this:
Where
welcome
is the name of the controller where your offline action is located.Does this help any?
这取决于
registration_path
的配置,即您在路由中定义的内容,但此处未进行解释。另请检查 rake paths 的输出,看看它在您的环境中是如何解释的。谨慎的做法是检查
log/development.log
以了解在显示解释的params
时如何处理表单提交。至于
资源
是什么,它来自Devise还是您的应用程序?This would depend on what
registration_path
is configured as, something you've defined in your routes but not explained here.Also check the output of
rake routes
to see how it's interpreted in your environment. It would be prudent to checklog/development.log
to see how the form submission is being handled as the interpretedparams
are shown.As to what
resource
is, does that come from Devise or your application?