Embarrassingly Simple Question

104 views
Skip to first unread message

Jay Garnett

unread,
Jul 5, 2014, 2:45:52 AM7/5/14
to rubyonra...@googlegroups.com
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:

<a href="/demo/hello">Hello page 1</a><br />
<%= link_to('Hello page 2', {:action => 'hello'}) %><br /> 

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.

Walter Lee Davis

unread,
Jul 5, 2014, 8:34:50 AM7/5/14
to rubyonra...@googlegroups.com
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?)

What does your routes.rb file look like?

Walter

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/20843245-d2fc-431d-aa30-5601778f982d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jay Garnett

unread,
Jul 5, 2014, 11:00:37 AM7/5/14
to rubyonra...@googlegroups.com
Thanks Walter, for your reply.

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:

Rails.application.routes.draw do

  root "demo#index"
  
  #get 'demo/index'
  match ':controller(/:action(:id))', :via => :get

end

Thanks in advance for your help!

Colin Law

unread,
Jul 5, 2014, 11:03:26 AM7/5/14
to rubyonra...@googlegroups.com
On 5 July 2014 07:45, Jay Garnett <c340...@gmail.com> wrote:
> 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:
>
> <a href="/demo/hello">Hello page 1</a><br />
> <%= link_to('Hello page 2', {:action => 'hello'}) %><br />

Have you copied/pasted that line from your code? If so then please do.

Colin

Jay Garnett

unread,
Jul 5, 2014, 11:13:31 AM7/5/14
to rubyonra...@googlegroups.com
Colin-

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:

<h1>Demo#index</h1>
<p>Hello from index!</p>

<a href="/demo/hello">Hello page 1</a><br />
<%= link_to('Hello page 2', {:action => 'hello'}) %><br />

Is this what you were asking for?

Thank you for your help.

Colin Law

unread,
Jul 5, 2014, 11:46:31 AM7/5/14
to rubyonra...@googlegroups.com
On 5 July 2014 16:13, Jay Garnett <c340...@gmail.com> wrote:
> Colin-
>
> 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:
>
> <h1>Demo#index</h1>
> <p>Hello from index!</p>
>
> <a href="/demo/hello">Hello page 1</a><br />
> <%= link_to('Hello page 2', {:action => 'hello'}) %><br />

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.

Colin

Jay Garnett

unread,
Jul 5, 2014, 11:50:30 AM7/5/14
to rubyonra...@googlegroups.com
Thanks, Colin.  Yes, I copied and pasted my code.

And I do understand what you're saying. :)

Colin Law

unread,
Jul 5, 2014, 12:13:21 PM7/5/14
to rubyonra...@googlegroups.com
On 5 July 2014 07:45, Jay Garnett <c340...@gmail.com> wrote:
> 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:
>
> <a href="/demo/hello">Hello page 1</a><br />
> <%= link_to('Hello page 2', {:action => 'hello'}) %><br />
>
> According to the tutorial, both links, once rendered, should be identical,
> but they are not and I can't figure out why.

Is it possible you had an error at some point and the page has not
refreshed in the browser, try changing the Hello page 2 text just to
make sure.

Otherwise, what happens if you say
<%= link_to('Hello page 3', {:controller => 'demo', :action => 'hello'}) %>

I would not have thought you should need it but when something is not
working it can be instructive to try stuff.

Colin

tamouse pontiki

unread,
Jul 5, 2014, 1:47:49 PM7/5/14
to rubyonra...@googlegroups.com
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!

Jay Garnett

unread,
Jul 5, 2014, 2:18:27 PM7/5/14
to rubyonra...@googlegroups.com
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.

1) Modified the Hello page 2 link code to include a reference to the controller
2) Added a "Hello page 3" link as suggested by Colin.  It too renders a link to /demo and not to /demo/hello
3) Loaded the page in a different browser with a fresh cache
4) Rebooted my Mac, restarted MySql and Rails Server
5) 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.

Here is my current code:

index.html.erb

<h1>Demo#index</h1>
<p>Hello from index!</p>

<a href="/demo/hello">Hello page 1</a><br />
<%= link_to('Hello page 2', {:controller => 'demo', :action => 'hello'}) %><br /><br />
<%= link_to('Hello page 3', {:controller => 'demo', :action => 'hello'}) %><br />

hello.html.erb

<h1>Demo#hello</h1>
<p>Hello world!</p>

<%= 1+1 %>
<br />

<% target = "world" %>
<%= "Hello #{target}" %>

demo_controller.rb

class DemoController < ApplicationController

layout false

  def index
    render('index')
  end

  def hello
    render('hello')
  end

end

Here is what is being rendered when I load /demo/index

<h1>Demo#index</h1>
<p>Hello from index!</p>

<a href="/demo/hello">Hello page 1</a><br />
<a href="/demo">Hello page 2</a><br /><br />
<a href="/demo">Hello page 3</a><br />

Thanks again for the suggestions and help.

Colin Law

unread,
Jul 5, 2014, 2:25:27 PM7/5/14
to rubyonra...@googlegroups.com
On 5 July 2014 16:00, Jay Garnett <c340...@gmail.com> wrote:
> Thanks Walter, for your reply.
>
> 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 have not used rails 4.1, is there a change that says the one must
have appropriate routes setup for this to work?

Colin

Jay Garnett

unread,
Jul 5, 2014, 2:46:25 PM7/5/14
to rubyonra...@googlegroups.com
Here is my current routes.db:

Rails.application.routes.draw do

  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.

Walter Lee Davis

unread,
Jul 5, 2014, 2:49:44 PM7/5/14
to rubyonra...@googlegroups.com

On Jul 5, 2014, at 2:46 PM, Jay Garnett wrote:

> Here is my current routes.db:
>
> Rails.application.routes.draw do
>
> 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.

Walter

>
> On Saturday, July 5, 2014 1:25:27 PM UTC-5, Colin Law wrote:
> On 5 July 2014 16:00, Jay Garnett <c340...@gmail.com> wrote:
> > Thanks Walter, for your reply.
> >
> > 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 have not used rails 4.1, is there a change that says the one must
> have appropriate routes setup for this to work?
>
> Colin
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3d853b44-372e-4031-95b3-5e7ce1aac763%40googlegroups.com.

Hassan Schroeder

unread,
Jul 5, 2014, 2:56:19 PM7/5/14
to rubyonrails-talk
On Sat, Jul 5, 2014 at 11:46 AM, Jay Garnett <c340...@gmail.com> wrote:

> 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.

That *is* a routing problem.

And a quick test case using 4.0.x and 4.1.x shows different results
with that wildcard match - you'll get the URL you expect with 4.0.x.

Probably best to stop now, install 4.0.x and resume the tutorial :-)

FWIW,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Jay Garnett

unread,
Jul 5, 2014, 2:57:15 PM7/5/14
to rubyonra...@googlegroups.com
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.

Arup Rakshit

unread,
Jul 5, 2014, 2:58:26 PM7/5/14
to rubyonra...@googlegroups.com

> 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.
>
> Walter
>

What is the logic behind it ?

--
================
Regards,
Arup Rakshit
================
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.

--Brian Kernighan

Jay Garnett

unread,
Jul 5, 2014, 3:05:27 PM7/5/14
to rubyonra...@googlegroups.com
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...

Thanks for the lessons.

Colin Law

unread,
Jul 5, 2014, 3:41:11 PM7/5/14
to rubyonra...@googlegroups.com
On 5 July 2014 19:57, Jay Garnett <c340...@gmail.com> wrote:
> 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.

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.

Colin

Jay Garnett

unread,
Jul 5, 2014, 6:00:47 PM7/5/14
to rubyonra...@googlegroups.com
I agree and am in the middle of learning how to downgrade Rails. :)

Thanks again everyone!!

Scott Ribe

unread,
Jul 5, 2014, 6:17:30 PM7/5/14
to rubyonra...@googlegroups.com, Jay Garnett
On Jul 5, 2014, at 4:00 PM, Jay Garnett <c340...@gmail.com> wrote:

> I agree and am in the middle of learning how to downgrade Rails. :)

You might want to use RVM, then you can have multiple Ruby/Rails versions installed, and switch between them with minimal effort. (see rvm.io)

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Jay Garnett

unread,
Jul 6, 2014, 5:44:56 PM7/6/14
to rubyonra...@googlegroups.com, c340...@gmail.com
Thanks Scott.  I am currently using rbenv.

Jay Garnett

unread,
Jul 6, 2014, 5:47:18 PM7/6/14
to rubyonra...@googlegroups.com
This issue has been resolved.  I have spent a fair amount of time learning about the different versions of Rails including changing from one to another.  During this process, a new version of Rails became available, 4.1.4.  Out of curiosity, I tried my code with the newer version and the pages and links were rendered as expected.
Reply all
Reply to author
Forward
0 new messages