How can I pass instance variables to a HAML template on the command line?
The reason that both of these examples are not working is that you are attempting to access instance variables from a different class. The simplest solution is to define and use methods instead of attempting to access another classes instance variables as if they were your own.
I.E. in test.rb
def foo
'abc'
end
test.haml
!!!
%h1 Testing HAML CLI
%p= foo
How can I use haml within a coffeescript file?
The correct order would be home.js.coffeescript.haml
—you want the file to first be evaluated as Haml to give you your variables, then compiled as CoffeeScript, then finally served as JavaScript.
However, I strongly suspect that the Haml processor will choke on some CoffeeScript syntax. It would probably be safer to use ERB, which should work for your example.
Why is an instance variable used instead of a local variable in a module?
This roughly means that if @current_user
, as an instance variable of global request/response scope (Controller/View/Helpers), has already been defined, use it as current_user
. Otherwise, assign it the user that corresponds to the id stored in the session do what you do.
In other words, this is similar to:
def current_user
if @current_user # this will be false in every hit, for the same request
@current_user
else
User.find_by id: session[:user_id]
end
end
And this should be enough.
But then, you may use again current_user
in some other part of the code, let's say in the controller, for the same request.
This means, that current_user
will have to hit the DB again (caching aside). And you would like to avoid that, if you can. So, why not store the user in a global instance variable?
Thus:
def current_user
if @current_user # this will be false for the first hit, true for every subsequent request
@current_user
else
@current_user = User.find_by id: session[:user_id]
end
end
And since we do this, why not use the shorthand method for it?
So, finally:
def current_user
@current_user ||= User.find_by id: session[:user_id]
end
The answer is, you do this for reusability shake, within the same request/response.
Undefined local variable or method 'place'
Make sure you are using the proper haml syntax:
- @places.each do |place|
%tr
%td= place.name
%td= place.address
%td= place.latitude
%td= place.longitude
%td= t(place.type)
%td= link_to t(:destroy), place, method: :delete
%td= link_to t(:edit), edit_place_path(place)
Related Topics
What Is a Very Simple Authentication Scheme for Sinatra/Rack
Rails: Logging the Entire Stack Trace of an Exception
Pass Ruby Script File to Rails Console
Rails Get Index of "Each" Loop
Recurring Billing with Rails and Activemerchant: Best Practices, Pitfalls, Gotchas
Generate Array of All Letters and Digits
Using a Duration Field in a Rails Model
How to Understand Strptime VS. Strftime
Rails Console Is Not Outputting SQL Statements to My Development Log
Difference Between Block and &Block in Ruby
Gracefully Shutting Down Sidekiq Processes
Get File Name and Extension in Ruby
Capistrano Deploy Fails After I Changed the Repository Url
Sorting an Array of Objects in Ruby by Object Attribute
Should I Specify Exact Versions in My Gemfile