Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing local variables to a view from controller

I am not able for some reason to pass local variables to the show view...

In my controller i have simply:

def show
  render template: "books/show", :resource => "Some text"
end

In my view i print the following:

<h1>My local variable text: <%= resource %></h1>

And i am getting the following message:

undefined local variable or method `resource' for #<#<Class:0x00000118ebce90>:0x00000118ec3498>

I've tried the following syntaxes in the controller:

render template: "books/show", locals: { resource: "Some text" }
render template: "books/show", locals: { resource => "Some text" }
render template: "books/show", :locals => { resource: "Some text" }
render template: "books/show", :locals => { resource => "Some text" }

Without any luck...

Any clues ?

Thanks!

like image 274
user3442206 Avatar asked Sep 08 '14 18:09

user3442206


4 Answers

TL; DR

render locals: { resource: "Some text" }

Full

First I am wondering why would you need the template:. Are you on Rails 2.x? If not, then the :template option is no longer required. You should be able to get along fine with just

render "books/show"

Second do you need to specify the template? What is the controller you want to render from? If that's BooksController, then you don't need the template path either, which makes the line being just

render

That's without the variables yet. Now, I just checked, and a simple:

render locals: { resource: "Some text" }

as well as:

render 'books/show', locals: { resource: "Some text" }

works for me just fine. Maybe earlier Rails versions treated 'resource' as some kind of a keyword? Don't know, but the above Worksforme™ in both forms.

like image 155
silverdr Avatar answered Nov 09 '22 00:11

silverdr


I think it should be like

render 'books/show', :locals => {:resource => 'Some text'}

It works for me

like image 26
Pradnya Kulkarni - Telang Avatar answered Nov 09 '22 00:11

Pradnya Kulkarni - Telang


Passing Data from the Controller to the View

Here's a NovelController class, to be put into app/ controllers/novel_controller.rb.

class NovelController < ApplicationController
      def index
        @title = 'Shattered View: A Novel on Rails'
      end
end

Since this is the Novel controller and the index action, the corresponding view is in app/views/novel/index.html.erb

<h1><%= @title %></h1>

Output:

Shattered View: A Novel on Rails

The view is interpreted after NovelController#index is run. Here's what the view can and can't access:

  • It can access the instance variables @title, because they've been defined on the NovelController object by the time NovelController#index finishes running.
  • It can call instance methods of the instance variables @title.
like image 14
Naresh Kumar P Avatar answered Nov 08 '22 22:11

Naresh Kumar P


You can't pass local variables from a controller to a view. What you could do is define it as an instance variable and then it would automatically be available in the view.

@resource = @book

If you want to pass entirely different objects, then just define those instance variables differently.

like image 4
Ryan Bigg Avatar answered Nov 08 '22 23:11

Ryan Bigg