【问题标题】:Rails: Resource and FormsRails:资源和表单
【发布时间】:2011-07-13 16:32:38
【问题描述】:

前言:我不确定资源是什么。

我需要这个表单(通过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 %>

我的注册控制器:

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

【问题讨论】:

  • “Action”通常表示控制器上的公共方法。 before_filter 设置是过滤器,skip_before_filter 调整这些设置。
  • 谢谢,塔德曼。你知道我在问什么吗?我猜该应用程序要么不知道如何在提交表单时路由表单,要么找不到资源......但同样,我是 rails/programming 的新手。有什么想法吗?

标签: ruby-on-rails view resources devise


【解决方案1】:

Devise 中的“资源”是实际注册的内容。在您的情况下,它很可能是用户。但是,用户并未硬编码到设计中,因此您可以拥有多种类型的用户,例如管理员或编辑。在这种情况下,为了简单起见,当您阅读“资源”时,请考虑“用户”。

我的其余答案并不是真正的答案,只是要求提供更多详细信息。如果我能提供帮助,我会用正确的答案编辑这个:)

至于你的问题,我不是 100% 确定我明白你在问什么。您是尝试在离线页面上显示注册表单还是尝试将注册数据发送到离线页面?在任何一种情况下,您是否收到错误或诸如此类阻止您这样做?如果是,请发布错误或意外行为的详细信息。发布您的 routes.rb 也可能会有所帮助,具体取决于您的问题。

还有一件事我不清楚,离线页面是您的 rails 应用程序的一部分还是托管在其他地方?如果应用已关闭,将无法访问。

更新:

所以我的理解是您试图将用户注册表单放在离线页面上?如果是这样,试试这个。

在您的控制器中:

def offline
  @user = User.new
end

在您的 offline.html.erb 视图中:

    <%= form_for(@user, :url => user_registration_path) do |f| %>
      <%= devise_error_messages! %>

      <p><%= f.label :email %><br />
      <%= f.email_field :email %></p>
      <p><%= f.submit "Sign up" %></p>
    <% end %>

我认为您没有为离线操作设置路线,因此您需要这样做。要获得一种快速简便的方法,请使用以下内容:

match '/offline' => "welcome#offline"

其中welcome 是您的离线操作所在的控制器的名称。

这有帮助吗?

【讨论】:

  • 我正在尝试从我的默认“sign_up”页面以外的页面注册。我不认为我正在尝试将注册数据发送到离线页面...查看我的路线更新。
  • 我已经更新了我的答案,如果有帮助或者我误解了,请告诉我。
【解决方案2】:

这取决于 registration_path 的配置,您已在路由中定义但此处未解释。

还要检查rake routes 的输出,看看它在您的环境中是如何解释的。谨慎的做法是检查log/development.log 以了解如何处理表单提交,如解释的params 所示。

resource 是什么,是来自 Devise 还是你的应用程序?

【讨论】:

  • 我很确定这是设计。谷歌返回的主要是设计文章。我知道我可能需要修复我的路线才能使其正常工作,但我也需要弄清楚资源问题,对吧?
  • Tadman,感谢您的支持。然而,我仍然很困惑——我应该在几天前完成这件事,这是唯一让我难过的事情。如果可以的话,您能否再扩大一下您的答案?
  • 您可以尝试使用devise 重新标记问题,并在重要时提供赏金。我对 Devise 的内部结构不太熟悉,无法确定那里发生了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 2014-04-07
  • 2014-12-05
  • 1970-01-01
  • 2015-02-18
  • 2015-09-30
相关资源
最近更新 更多