0

Im reading an old tutorial about instance variables.

However i wasnt able to call de instance variable from the CONTROLLER to the VIEW.

The code is:

VIEW :

<html>
  <head>
    <title> title </title>
  </head>
  <body>
    <p>            line 1       </p>
    <p> <%=       @text   %>    </p>

  </body>  
</html>

CONTROLLER:

class MoviesController < ApplicationController
  def movies
    @text = "movietitle"
  end
end

When i load the page, it only shows the line 1 paragraph.

How can i call the instance variable @TEST?

8
  • changed the title because the older sucked Commented Jul 25, 2011 at 1:51
  • 1
    are you even at the movies/movies page? Commented Jul 25, 2011 at 1:55
  • What are the file names and locations you're using? Commented Jul 25, 2011 at 2:01
  • Well if he says he sees the line 1 paragraph I assume he is rendering the right file, but its still odd. Could you please copy + paste the html source code that the browser is displaying. Commented Jul 25, 2011 at 2:08
  • Make sure that the line @text = "movietitle" gets executed(may be by putting debugger in the first line of the movies action. Commented Jul 25, 2011 at 5:43

2 Answers 2

2

The @test variable should contain "movietitle", and it should properly print both paragraphs.

Maybe the extra space (because of pretty printing) is screwing things up? Try:

<p>line 1</p>
<p><%=@text%></p>

EDIT:

for what you showed, your controller method must be named index not movies

Sign up to request clarification or add additional context in comments.

5 Comments

Wasn't me but <%= @text %> and <%=@text%> should do exactly the same thing.
I thought the same thing, but again there's nothing else that rings a bell on what the OP showed :S
Downvote unnecessary, upvoting to correct it. I think the question is unclear from what he has shown. The issue is probably along filenames, his route etc.
I havent been able to make the instance variables work. I am doing the following tutorial and it runs ok. fairleads.blogspot.com/2007/12/… but in the end i want to call an instance variable from the CONTROLLER to the VIEW and it doesnt load. i have uploaded this video if you can help me. youtube.com/watch?v=xixCNjcpxUQ Thanks!
1

Ok based on your video, here is your problem.
You add a new controller method to movies called newaction. In your browser you go to /movies which effectively, calls the controller method index for movie and renders the index.hmtl.erb for movies. Inside index.html.erb you try to get the variable you declared in newaction, this is another completely different action and of course anything in newaction is not accesible from the index view. The biggest problem is you seem to think controller methods are like functions which are called from view, they are not.

  1. Read how the MVC model works: http://guides.rubyonrails.org/getting_started.html
  2. You have to create a newaction.html.erb view in movies
  3. You need to add the newaction action to your routes.rb file

Read this: http://guides.rubyonrails.org/routing.html

The code in routes.rb will look like.


resources :movies do
  get 'newaction'
end

if you do these and you go to /movies/newaction and inside newaction.html.erb display the title, it will work.

3 Comments

OK I added the code and a link to a guide that is a definitive read.
Hi daniel, I do what you said and it return me -> undefined method resources. I have uploaded this video if you can help me. youtube.com/watch?v=PC-OBxri5Jo Thanks.
resources was added in rails 3. You are using rails 2. Sorry can't help you with rails 2, you can try googling how rails 2 routes work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.