Routing in Rails making the Username an URL:

后端 未结 1 1111
难免孤独
难免孤独 2021-01-07 08:20

In my Rails App there is Device Model - User, and a Registry model( Each user has one registry).

I wanted to change my routes so that instead of:

\"http://lo

1条回答
  •  鱼传尺愫
    2021-01-07 08:58

    When you create or update your models, you send POST /registries or PUT /registries/1.

    But /registries is matched by your last rule match '/:name' => "registries#show", so the request hits the show action.

    If you run rake routes you should see something like this:

                    POST   /registries(.:format)                 registries#create
                    PUT    /registries/:id(.:format)             registries#update
                    DELETE /registries/:id(.:format)             registries#destroy
                           /:name(.:format)                      registries#show
    

    You can add method parameter to your route, so that it will hit show only on GET request.

    match '/:name' => "registries#show", :via => :get
    

    But there are still can be collisions in the future. For example, if you have registry name users. So, it's commonly suggested to use prefixes (match '/r/:name') or define set of allowed names, or choose safe names for registries.

    P.S. I don't think load_and_authorize_resource will work for your show action by default. Because it expects params[:id] to load the resource automatically.

    0 讨论(0)
提交回复
热议问题