I’m the ultimate newb on ROR. I am following through a tutorial on Lynda.com called “Ruby on Rails 4 Essential Training”. I am learning about the MVC structure. They show a web page that has the following links:
I'm the ultimate newb on ROR. I am following through a tutorial on Lynda.com called "Ruby on Rails 4 Essential Training". I am learning about the MVC structure. They show a web page that has the following links:
According to the tutorial, both links, once rendered, should be identical, but they are not and I can't figure out why.
This is what is being rendered:
<a href="/demo/hello">Hello page 1</a><br
/>
<a href="/demo">Hello page 2</a><br />
Again, the tutorial shows the second link as /demo/hello. Why doesn't this link to the hello action the way it should?
Here is the controller, if that helps:
class DemoController < ApplicationController
layout false
def index
render('index')
end
def hello
render('hello')
end
end
Thanks for your help.
What version of Rails are you using, and is it exactly the same version as specified in the tutorial? (Patch level doesn't matter as much as major and minor version.)
Since you're not getting an error, it's hard to say why this is working out this way. Are you running the code using a server in your Terminal? (Did you start the server with rails server in the project directory?)
The tutorial is a year or so old. They are using a slightly older version, but still 4.0+.
I am running the current stable build of Rails, which is 4.1.2. For a webserver, it was recommended that I run what they are running in the tutorial, which is WEBrick.
I installed everything within the past week, so all software components should be current.
I am running “rails s” in Terminal and it shows WEBrick running and logs info when pages are accessed, redirected, etc.
My routes.rb file is the installed default. Here it is:
I’m not sure I understand your question 100%. I posted both the template code and the html code that was being rendered. In addition, I posted the Controller code. I did not, however, post the entire template code, but the code that was left out shouldn’t be relevant. Here it is:
Sorry for not being clearer. What I meant to ask was whether you
actually copied that (Ctrl C) from your sourceand pasted it into the
post (Ctrl V), or did you re-type it? If you did not copy/paste then
there is always the possibility of your code having a typo that you
have not noticed. You may say "of course I copied/pasted" but you
might be surprised how many times people ask a question only to
eventually discover that the code they posted is not exactly the same
as the code they have used, so I have to ask.
Can I just back up here and say your question is not simple, you should not
be embarrassed to ask it, and it's quite appropriate. When doing online
tutorials, and you get stuck like this, this is one of the places to come
and ask. I'm glad you asked, and I'm glad you're taking the time to learn!
Thank you for your kind comments. At first blush, I thought the problem was something simple like a setting or something a new user wouldn’t automatically know, but would be obvious to ROR pros.
The bottom line is my code is supposed to be linking the user to the hello.html page, but it doesn’t. It links them to the “root” of the site which causes it to load the index.html page. Not the intended result.
Here is what I have done, based on suggestions here.
Modified the Hello page 2 link code to include a reference to the controller
Added a “Hello page 3” link as suggested by Colin. It too renders a link to /demo and not to /demo/hello
Loaded the page in a different browser with a fresh cache
Rebooted my Mac, restarted MySql and Rails Server
Tried the page again and saw the same result
I don’t get any errors or any sign that something is amiss. The code just renders href links to a destination other than what is desired.
But I don’t think routing is the problem. The rendered links are the problem as they aren’t pointing to the right URL in the first place. When the user clicks on the link, they are being routed correctly but the link isn’t pointing where it was intended to point.
root "demo#index"
match ':controller(/:action(:id))', :via => :get
end
But I don't think routing is the problem. The rendered links are the problem as they aren't pointing to the right URL in the first place. When the user clicks on the link, they are being routed correctly but the link isn't pointing where it was intended to point.
Try putting your root directive as the very last thing in the routes file. That's where it belongs, as it's the last option possible.
Done. No effect. As I mentioned, I don’t think it’s a routing problem. It is a rendering problem. Somewhere in the Rails system where it renders view template code, it is not rendering the expected html link.
Thanks again for your continued interest and suggestions.
Ok, so when an html is rendered and links are created, the Rails system refers to the routes.db to create the links? I understand now, I thought that the routes.db came in to play when a link was clicked on and routes.db assisted in the routing.
Very cool. Very interesting. Fairly frustrating that I can’t get this to work the way it should…
I think the suggestion to switch to the correct version of rails for
the tutorial is the way to go, otherwise you will keep falling over
this sort of issue.