Rails path nomenclature not working - ruby-on-rails

I have a Fixer model, but, unlike all the other models in my app, its routing isn't working right, even though there's a resource line for it in the routes file.
The problem is, when I try to link to the basic show path any number of ways, like
<%= link_to "Fixer", fixer_path(#fixer) %>
or
<%= link_to "Fixer", #fixer %>
or
<%= link_to "Fixer", fixer_path(#fixer.id) %> # I got desperate
it links to /fixers.[:id] (not a real page) instead of /fixers/[:id]. No idea what's happening, cause my resources line is there, and show is a basic resources action, and all the other similarly resourced models seem to be working just fine.
Any ideas? (I can put more code up if necessary. Just not sure what would be relevant).
EDIT -- The Fixers output in my rake routes:
fixers GET /fixers(.:format) fixers#index
POST /fixers(.:format) fixers#create
new_fixer GET /fixers/new(.:format) fixers#new
edit_fixer GET /fixers/:id/edit(.:format) fixers#edit
GET /fixers/:id(.:format) fixers#show
PUT /fixers/:id(.:format) fixers#update
DELETE /fixers/:id(.:format) fixers#destroy
Whoa. Just noticed after posting this that the 5th line is missing the "fixer" before the show action line that all my other models have. Why would that have happened? How do I fix it?
EDIT -- I figured it out! Really dumb issue. For some reason, back when I was learning how to do all this, I both included a resources line and added this line above it:
match '/fixer', to: 'fixers#new'
When I took that line out (cause it was redundant), the problem went away. I guess I was messing with the Rails routing automagic. They really do make those defaults the best option.

Hey I have had this same problem before.. and unfortunately have resorted to doing the following: link_to "link text", "/fixers/#{#fixer.id}" I would love to know the actual correct answer for this though.

Related

Ruby link helper using the routes file

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.

Inherited app with strange routing error, No route matches {}

Whenever one of the mailers fires off if the body contains something like link_to root_url it gives this incredibly unhelpful error:
ActionView::Template::Error: No route matches {}
The trace either is useless or points to that link_to method. Since it's also very unsearchable here I am asking on stack overflow.
UPDATE 1: To help, I've posted the terrifying routes.rb file: https://gist.github.com/2955610
UPDATE 2: In the console, app.root_url provides the correct return.
UPDATE 3: link_to "whatever", root_url works just fine. So strange!
UPDATE 4: It works fine in a regular rails view.
UPDATE 5: FIXED The image_tag now isn't providing a host in the email, outputting: http://assets/...png. Fffff.
UPDATE 6: I've narrowed it down to that I get the same error with (in console) app.url_for. I think it may be related.
So the real answer is that link_to works differently in ActionMailer than in ActionController. The difference is that apparently ActionController has an option called :script_name, and this makes all sorts of assumptions for you.
Now while I don't agree with that difference there's not much you can do about it, so here's what I ended up doing:
link_to root_url, root_url
Now, why would I use both? Well two reasons:
I wanted the url as the anchor text and the href.
If the URL changes, I don't want to have to update all those strings.
The problem with mailers is that maybe you miss "host" option? :)

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.

Using Ruby on Rails link_to to link to controller action

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.

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