开发者

RESTful Routes help in Rails 3

开发者 https://www.devze.com 2023-03-01 23:35 出处:网络
I\'m using Rails 3, Devise and Mongoid. I believe I can accomplish what I need using RESTful Routes but I am not sure how to do it. Let me explain what I have and what I am trying to do.

I'm using Rails 3, Devise and Mongoid.

I believe I can accomplish what I need using RESTful Routes but I am not sure how to do it. Let me explain what I have and what I am trying to do.

Let's say I have two controllers - User and Simpleform.

The Simpleform is a public facing form (no authentication required) that when submitted shows up in a user's account (when they login).

I have multipul users on the system and each one will see the form submissions that's specific to them.

So the question is, how do I get the public facing form to submit to a specific user's account?

As of now the route to fill out a new form looks like this "site.com/simpleform/new". I think I can use routes to make it look like this "site.com/simpleform/user_id/new" or "site.com/user_id/simpleform/new". Either variation would work I think. Now when the form is submitted from someone in the public the application knows what user to associate it too because of the user_id in the url.

开发者_JAVA百科

I think that logic works and RESTful Routes can make it happen, I'm just not sure how to do it.


Each User resource has one associated SimpleForm resource.

So I would think your route would be like:

  resources :users do
    resource :simpleform
  end

And routes would look like:

     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

精彩评论

暂无评论...
验证码 换一张
取 消