I have this route defined in my routes.rb:
matc "articles/:id/vote/up" => "articles#vote", :liked=>true
The rake for this route is:
/articles/:id/vote/up(.:format) jokes#vote {:liked=>true}
With the first column being blank.
My question is, what would the route helper name be for this link?
For example articles_vote_up_path(:id).
Is there an uglier way to link to this path?
How can I find out?
I'm thinking I misunderstood something, please guide me in the correct direction in that case.
Thank You
you need to pass an :as option to use a named route
match "articles/:id/vote/up" => "articles#vote", :liked=>true, as: :article_vote_up
which generates an article_vote_up_path
When you run rake routes, you can add _path to the whatever's in the first column on the left, and that's what the route url helper is for that link. For example, here's a line from rake routes on one of my projects:
subscriptions GET /subscriptions(.:format) subscriptions#index
The url helper for this url would be subscriptions_path.
Related
Couldn't find answer searched everywhere. Maybe I am using rails incorrectly.
I have made a custom route:
get "/posts/:page/category/:query" => 'posts#index', as: :posts_category
As you can see above I have customized in my route a path with a 'category' string within it.
How do I use link_to to address this.
I have tried
link_to posts_category(:page=>1, :query=>3)
This doesn't work because the link created by rails does NOT include the the /category/ within the path. How do I add that in the link_to.
Kind Regards,
Your code works fine (although I'm assuming it was a typo that you wrote posts_category() instead of posts_category_path()), so maybe you have another route defined earlier in your routes.rb with the same helper name, it will be masking this one.
Run rake routes and look for something else with the posts_category helper.
I am trying to create a scaffold called 'Pages'.
So far everything is fine but the page structure now needs to change where it currently is:
http://0.0.0.0:3000/pages/the-page-name
What I need to do now is have this instead:
http://0.0.0.0:3000/the-page-name
In my routes.rb I have this:
resources :pages
This obviously maps all routes within the model to this base but I want to hide this.
Is it entirely possible?
Thanks,
Taken from the Rails Routing Guide, you could do this:
get '*pages', to: 'pages#show', format: false
I would recommend you make it the very last route you have, since the Rails router matches the request with the first route, and having a wildcard early in your routes file will end up clobbering all your other routes/resources.
You can specify a path
resources :pages, path: ''
I have a resource in a Rails 3.2 app called "news". I know it is not the best name to use for a resource but I'd prefer not to change it.
I have an issue with the routing since new_news_path doesn't work.
How can I define a custom route for the new action? something like unused_news_path for news#new?
thanks for your help.
Try this in your routes.rb:
match "unused" => "news#new"
When you do a rake routes | grep unused, you will get the following output:
unused /unused(.:format) news#new
So you now can use unused_path on your views and controller to get to the correspondent action.
In my Rails 3.2 application, there is an Exercise model with attributes muscle_group and grade_level. I've defined the following route with dynamic segments for it in config/routes.rb:
# config/routes.rb
match "/:muscle_group/grade-:grade_level/:id" => "exercises#show"
Running bundle exec rake routes confirms that the route does indeed exist:
/:muscle_group/grade-:grade_level/:id(.:format) exercises#show
The database contains an Exercise record with:
id = 5
muscle_group = "abdominal"
grade_level = 1
And yet when I point my browser to http://localhost:3000/abdominal/grade-1/5, I get:
Routing Error
No route matches [GET] "/abdominal/grade-1/5"
Try running rake routes for more information on available routes.
How can I get this route with dynamic segments to work?
The route declaration I was using was almost functional. For some reason there is an issue with using a hyphen and not having grade_level as its own /-separated substring. When I switched to using the route declaration:
match "/:muscle_group/grade/:grade_level/:id" => "exercises#show"
Instead of the original:
match "/:muscle_group/grade-:grade_level/:id" => "exercises#show"
http://localhost:3000/abdominal/grade/1/5 is then a valid route.
I would prefer to have this route with the hyphen and wish I knew how to make that work, but it is not a top priority. If anyone knows how to make it work with the hyphen, please let me know.
Action pack in Rails uses the to_param to override how the urls get generated by the URL helpers. Take a look at this article, which explains it.
http://www.seoonrails.com/to_param-for-better-looking-urls.html
Is there a method like "full_url" such that #comment.full_url or full_url_for(#comment) returns "http://www.host.com/comments/id" where www.host.com is the default host domain and id is #comment.id. Or, if not, what would be an elegant way to generate this url string?
I'm pretty new at Rails, most of the methods I've learned insert the tag and other markup.
url_for is not helping because I can't do something like the following:
url_for(#comment, {:only_path => false})
I've spent way too much time trying to figure this out. It came down to either hacking or asking for the right way on SO. Here I am.
If you are setting up your routes correctly in your config/routes.rb file then you should have access to named routes in your controller and in your views. Which should mean that all you should need to do is:
comment_path(#comment)
Or for the full url
comment_url(#comment)
To see a list of all of the routes from the command line, you can type rake routes from the project root. Welcome to rails! Here is a good resource for rails 3 routing: http://guides.rubyonrails.org/routing.html
some additional resources via Railscasts:
http://railscasts.com/episodes/231-routing-walkthrough
http://railscasts.com/episodes/232-routing-walkthrough-part-2