Chinu Chinu - 6 months ago 13
Ruby Question

How to get automatically page URL in ruby like CakePHP?

I am beginner in Ruby on Rails. I have an

users_controller.rb
there have lots of
action
approximately 100 more. So it's hard to set in the routers. I want to automatically set action.

Example:

def abc
render text: 'Hello World!"
dnd
...
...
def xyz
render text: 'Bla Bla Bla'
end


I want my URL should be open this type

http://localhost:3000/users/abc
http://localhost:3000/users/xyz
http://localhost:3000/users/some-action..


No need to set any multi line URL path in routers.

Here below I was write in router. Bu that code not working:

match ':controller(/:action(/:id(.:format)))'


Please help me

Answer

The error I was getting using your code was :

You should not use the `match` method in your router without specifying an HTTP method. (ArgumentError)
If you want to expose your action to both GET and POST, add `via: [:get, :post]` option.

On Rails 4.2.5.2 and ruby 2.3.0 the following works :

match ':controller(/:action(/:id(.:format)))', via: [:get, :post]

Please note the , via: [:get, :post].