2011-04-28 91 views
0

我正在使用Rails 3,Devise和Mongoid。RESTful路线帮助Rails 3

我相信我可以用RESTful Routes完成我所需要的工作,但我不知道该怎么做。让我解释我有什么和我正在尝试做什么。

比方说,我有两个控制器 - 用户和Simpleform。

Simpleform是一种面向公众的形式(无需认证),当提交时显示在用户帐户中(登录时)。

我在系统上有多个用户,每个用户都会看到特定于他们的表单提交。

所以问题是,我如何获得面向公众的表单提交给特定用户的帐户?

截至目前,填写新表单的路径如下所示:“site.com/simpleform/new”。我想我可以使用路线使其看起来像“site.com/simpleform/user_id/new”或“site.com/user_id/simpleform/new”。任何变化都会起作用。现在,当表单由公众中的某个人提交时,由于url中的user_id,应用程序也知道要将其关联的用户。

我认为逻辑工作和RESTful路线可以使它发生,我只是不知道该怎么做。

+0

你能澄清,请。你说不需要认证提交Simpleform,但你希望它与用户相关联?不知何故,你必须提供一个用户ID来将Simpleform与用户关联起来。当他/她提交表单时(一个简单的嵌套资源),用户必须登录。或者表单必须包含用户ID字段(允许任何人将简单表单​​与任何用户关联)。你希望在用户访问并提交SimpleForm之前要求用户登录吗? – 2011-04-28 22:00:04

+0

或者你想让任何人看到SimpleForm,然后在他们尝试提交表单时被迫登录? – 2011-04-28 22:01:41

+0

都不是。假设你有公司A和“公众(很多人)”。公司A有一个我的应用程序的帐户,并有一个表格,他们希望把他们的网站。任何人都可以填写A公司网站上的表格并提交(不需要认证)。但是,公司A需要登录系统才能看到提交的表单。有几家公司在我的应用程序中有帐户,所以我需要一种方法来了解何时向公众提交一份公开的表单。因此,公司ID在表单的url中的解决方案。这是否更有意义? – sevens 2011-04-28 22:10:00

回答

1

每个用户资源都有一个关联的SimpleForm资源。

所以,我觉得你的路线是这样的:

resources :users do 
    resource :simpleform 
    end 

和路线将如下所示:

 user_simpleform POST /users/:user_id/simpleform(.:format)     {:action=>"create", :controller=>"simpleforms"} 
new_user_simpleform GET /users/:user_id/simpleform/new(.:format)    {:action=>"new", :controller=>"simpleforms"} 
edit_user_simpleform GET /users/:user_id/simpleform/edit(.:format)    {:action=>"edit", :controller=>"simpleforms"} 
        GET /users/:user_id/simpleform(.:format)     {:action=>"show", :controller=>"simpleforms"} 
        PUT /users/:user_id/simpleform(.:format)     {:action=>"update", :controller=>"simpleforms"} 
        DELETE /users/:user_id/simpleform(.:format)     {:action=>"destroy", :controller=>"simpleforms"} 
       users GET /users(.:format)          {:action=>"index", :controller=>"users"} 
        POST /users(.:format)          {:action=>"create", :controller=>"users"} 
      new_user GET /users/new(.:format)         {:action=>"new", :controller=>"users"} 
      edit_user GET /users/:id/edit(.:format)        {:action=>"edit", :controller=>"users"} 
       user GET /users/:id(.:format)         {:action=>"show", :controller=>"users"} 
        PUT /users/:id(.:format)         {:action=>"update", :controller=>"users"} 
        DELETE /users/:id(.:format)         {:action=>"destroy", :controller=>"users"} 
+0

这工作。我必须弄清楚表单的不同方面的一些认证权限(编辑vs新的vs show等) - 但这不是这个问题的一部分。谢谢您的帮助。 – sevens 2011-04-28 22:30:34