Tuesday, 15 September 2015

How to handle get and post requests with a single line in routes.rb in rails 4 -



How to handle get and post requests with a single line in routes.rb in rails 4 -

how handle , post requests in single line.

i have 1 definition(test) in controller(accounts). definition can called either request or post request. define in single line in routes.rb file.

now doing like,

resources :accounts do

collection 'test' post 'test' end

end

which worst case. if have more definitions in controller need mention 2 time each , every definition.

please provide me solution.

you should utilize get, post, put, patch , delete methods constrain route particular verb. can utilize match method :via alternative match multiple verbs @ once:

match 'photos', to: 'photos#show', via: [:get, :post]

you can match verbs particular route using via: :all:

match 'photos', to: 'photos#show', via: :all

for example:

match '/list' => 'products#products_list', :as => 'products_list' , via: [:get, :post]

reference

ruby-on-rails

No comments:

Post a Comment