简体   繁体   English

rails路由控制器动作变化

[英]rails routing controller action change

I'm struggling here with a problem: I have a controller questions which has action new . 我在这里遇到了一个问题:我有一个控制器问题 ,其中有新的动作。 Whenever I need to create new question, I'm typing 每当我需要创建新问题时,我都在打字

/questions/new

What changes to routes.rb should I make to change the URI to 应该对routes.rb进行哪些更改以将URI更改为

/questions/ask

Thank you. 谢谢。 Valve. 阀。

Try this: 尝试这个:

map.ask_question   '/questions/ask', :controller => 'questions', :action => 'new'

Then you'll have a named route and you can: 然后你将有一个命名路线,你可以:

link_to "Ask a question", ask_question_path

If you are using RESTful routes maybe you'd like to use map.resources for your questions . 如果您正在使用RESTful路线,那么您可能希望使用map.resources来解决问题

To rename the action urls you may do this: 要重命名操作网址,您可以执行以下操作:

map.resources :questions, :path_names => { :new => 'ask', :delete => 'withdraw' }

(I added delete for the sake of the example) (为了示例,我添加了删除)

Which version of rails? 哪个版本的导轨?

Generally the default route should catch anything like /:controller/:action, so you could just create an ask method in your questions controller. 通常,默认路由应该捕获/ / controller /:action之类的内容,因此您可以在问题控制器中创建一个ask方法。 Take a look at the api documentation for named_route and map_resource if you want something a bit smoother to work with. 如果你想要更平滑的东西,请查看named_route和map_resource的api文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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