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.
Related
I'm having trouble creating routes for my Rails app and google is returning mostly results for rails 2-3 from 5 or more years ago.
I'm trying to create a "show" route without using resources, as I need multiple show views.
If I do the following :
get "/parameters/:id" => "parameters#show"
Then if I directly type in the path, like "parameters/12" it will work, but when I run rake routes it doesn't show a path next to the route. And I can't seem to get a path to work for it.
I'm sure I'm forgetting something really obvious here, but I've been at this problem for a few hours and haven't found a solution so I would really appreciate some help.
Because you are defining a custom route, Rails doesn't know how to generate the _url and _path named routes for you automatically.
In order to get the named routes generated, you need to help Rails by specifying the as argument
get '/parameters/:id', to: 'parameters#show', as: 'parameters_show'
Okay so I know this is very basic but I have searched everywhere and I cannot find a solution.
This is my routes file:
match '/interestonly' => 'interestonlymortgagecalculators#interestonlymortgagecalculator'
How do I create the link to link to this? I taught it would be as simple as
<%= link_to 'Interest Only Mortgage Calculator', interestonly %>
Can someone please help me I know this is very basic stuff but I can;t find anything on it, possibly due to my wording of the question
You'll want to read this over: Ruby on Rails Guides: Rails Routing from the Outside In. I've linked directly to the relevant section.
What you want is the path helper, which you can access by running rake routes in the command line. The path helper will be in the left column. After your comma in the code above, you should type in the path helper.
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.
I'd just started toying around with Ruby on Rails and had come across an issue with linking to another action in a controller from a particular view. I am almost certain it's an issue (or lack of code) in my routes.rb file, but I think I'm misunderstanding exactly how this file works & what I have to do. I've got a solution but pretty sure it's not the "best way" to do it.
I have one controller called home with two actions, index (which is the default) and newbill. Inside index.html.erb I have:
<h1>Home View</h1>
<%= link_to "new", :controller => "home", :action => "newbill" %>
However I was getting a routing error:
No route matches {:controller=>"home", :action=>"newbill"}
Doing rake routes gives me the following:
root / {:controller=>"home", :action=>"index"}
I then (following some Googling) added this code to routes.rb
match 'home/newbill' => 'home#newbill', :as => :newbill
And then in my index.html.erb I've got this:
<%= link_to "Name", newbill_path %>
And now this works as expected. My questions however are:
Why does this work? What exactly is going on behind the scenes?
Surely this is not the best way to do it? Adding another match 'home/newbill'... for every controller / action I want to link to seems a rubbish way of doing things.
I really like Ruby, but struggling a bit with this aspect of Rails...routing in general is messing up my head a bit I think!
Any help is much appreciated :D
Thanks,
Jack
I guess the first time your code didn't work because your home controller is defined as a resource.
If you define a controller as a resource in routes.rb file it will support only 7 standard methods (according to REST architecture):
index
new
create
show
edit
update
destroy
If you need any more custom routes you should add them manually, say in your case 'newbill', may go as:
resources :home do
collection do
get :newbill
end
end
But as per my understanding, your newbill method should go to bills controllers new, method not in the home controller.
You are right, Rails routes are little bit confusing (at least for me), but once you understand you can do lots of cool stuff.
Read here for the Rails official routes documentation:
http://guides.rubyonrails.org/routing.html.
You should check out the Rails Routing guide. A read through will help you understand what is going on behind the scenes.
This works becuase rails filters every request through the router looking for a match. This enables you to define custom routes such as domain.com/post when the path is actually blog#post. Prior to rails 3, a catch-all route was the last route in the routes file. This allowed you to define a controller and action and it would just work. I'm on my iPad and not near any projects, so I can't verify it, but I think that route is still there in rails 3.1, it just needs to be umcommented.
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