Ruby link helper using the routes file - ruby-on-rails

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.

Related

Rails "Show" routes

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'

Link_to & Routes Relation: How to customize a hard path within LinkTo

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.

What do I need to do to make this link work in Rails

I am trying to learn ruby and learn how to handle a while request round trip.
On my index.html.erb page I added this line:
<%= link_to "Alex Link", test_path(#test) %>
but I got an error:
undefined method `test_path' for #<#<Class:0x4064e80>:0x3c0b5c8>
As I understand it, I need to add a record to routes.rb, and then a controller. Correct? How do I do that?
I read the explanation for this in the Rails Guides, but just finding it a bit confusing doing it the first time.
For your purposes (learning) resources tests if fine.
It also gives you other routes for free, see RESTful routes.

Generating an object's absolute url without html markup

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

Ruby on Rails, from an index action linking to individual item error

I'm working on a ruby on rails project currently, doing it without scaffolding or anything just by hand. I have an index controller that simply lists all existing items in the db with a link on each that redirects to a details view.
The error I am getting is when trying to link to the item I have this line of code:
<%= link_to "Show", person %>
this is the same line of code I see everywhere, even in other working apps, I know person is the right variable name but no idea why this is failing. The error I get at runtime is:
undefined method `person_path' for #<ActionView::Base:0x7fe4d07f6568>
any helpful hints?
Check your "routes.rb" file. Make sure you have a declaration similar to:
map.resources :people
If you open terminal and run "rake routes", it will show you all routes that are currently recognized by your app.
Sounds like the route for Person is not configured properly. Is the app using REST-ful routing? Read more at: http://guides.rubyonrails.org/routing.html

Resources