简体   繁体   中英

rails controller action routing

I'm looking to "automatically" generate routes based on actions defined in a controller. resources routing (as far as I know) only automatically generates routes & helpers for http verbs. In this instance I'm serving mostly static pages using rails and have no need for http verbs in my controller.

Specifically:

In a controller I have defined actions which refer to those mostly static pages.

def action
end

In the routes file I have a bunch of

match "/url" => 'controller#action'

I'd like all those matched routes to be generated automatically based on the actions in the controller. Something CONCEPTUALLY along the lines of:

for actions in controller.erb do |action|
    'match "/action" => "controller#action"
end

Is this possible? Would I write the code in the routes file directly?

I also have some nested actions to consider... a controller action may be:

def action
    def nested_action
    end
end

I'd appreciate any thoughts on this matter. Thanks.

What's wrong with the normal /:controller/:action idea?

That won't deal with nested actions, but... I'm having a difficult time understanding why you'd ever want that.

You can do something like this:

controller :controller_name do
   get "path/action" => :method_name, :as => :path_action
   post "path/save" => :method_name, :as => :path_save
end

That is, you can group different routes within a controller using the method above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM