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
@t开发者_如何学Goext = "movietitle"
end
end
When i load the page, it only shows the line 1 paragraph.
How can i call the instance variable @TEST?
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
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.
- Read how the MVC model works: http://guides.rubyonrails.org/getting_started.html
- You have to create a newaction.html.erb view in movies
- 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.
精彩评论