ruby on rails - problem with friendly URLs routing by name instead of ID -


I have a code that looks like this:

  map.app ' Application /: title_header ',: admin = & gt; 'Apes',: Action => 'Show'  

I also changed my show method in the controller like this:

  @app = App.find_by_title_header (params [: title_header] ) If I type in  Application / Title  then I work fine. If I use  applications / 1  (valid ID) If it does type in, it fails (as expected) 

But when I'm trying to use friendly url assistants, in this case App_path (@app) generated url Such as applications / 1 . The only way I can do this is by entering app_path (@ app.title_header) . It seems that if it "just knows" that it is being expelled from the app object, then it should "just know" that by default, the name should pass through instead. Why do I need to change?

The easiest, easiest solution is to use only:

 < Code> class application & lt; ActiveRecord :: Base def to_param title_header.parameterize end end  

string example method parameterize will ensure string URL is appropriate. After that, in your controllers, you only need to update all Find s to use find_by_title_header instead of search .

Make sure you have title_header s unique!


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -