Embarrassingly Simple Question

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:

Hello page 1

<%= link_to(‘Hello page 2’, {:action => ‘hello’}) %>

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:

Hello page 1
Hello page
2

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.

On Jul 5, 2014, at 2:45 AM, Jay G. wrote:

/>

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?)

What does your routes.rb file look like?

Walter

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-

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:

Demo#index

Hello from index!

Hello page 1

<%= link_to(‘Hello page 2’, {:action => ‘hello’}) %>

Is this what you were asking for?

Thank you for your help.

On 5 July 2014 16:13, Jay G. [email protected] wrote:

Hello page 1

<%= link_to(‘Hello page 2’, {:action => ‘hello’}) %>

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

On 5 July 2014 07:45, Jay G. [email protected] 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:

Hello page 1

<%= link_to(‘Hello page 2’, {:action => ‘hello’}) %>

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

Thanks, Colin. Yes, I copied and pasted my code.

And I do understand what you’re saying. :slight_smile:

On 5 July 2014 07:45, Jay G. [email protected] 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:

Hello page 1

<%= link_to(‘Hello page 2’, {:action => ‘hello’}) %>

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

Colin

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.

  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

Demo#index

Hello from index!

Hello page 1

<%= link_to(‘Hello page 2’, {:controller => ‘demo’, :action => ‘hello’})
%>


<%= link_to(‘Hello page 3’, {:controller => ‘demo’, :action => ‘hello’})
%>

hello.html.erb

Demo#hello

Hello world!

<%= 1+1 %>

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

Demo#index

Hello from index!

Hello page 1
Hello page 2

Hello page 3

Thanks again for the suggestions and help.

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.

On Jul 5, 2014, at 2:46 PM, Jay G. 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 Sat, Jul 5, 2014 at 11:46 AM, Jay G. [email protected]
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 :slight_smile:

FWIW,

Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan

On 5 July 2014 16:00, Jay G. [email protected] 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

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.

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

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

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.

On 5 July 2014 19:57, Jay G. [email protected] 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

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

Thanks again everyone!!

On Jul 5, 2014, at 4:00 PM, Jay G. [email protected] wrote:

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

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 R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs