pandaZW pandaZW - 1 month ago 6
Ruby Question

How to make out @variable and variable in Ruby?

To be compatible with other actions.I use

variable
(not
@variable
) in
_show.html.erb
.

<p><%= journal.title %></p>


But in
show.html.erb
, i can't turn
@variable
into
variable
and render to
_show.html.erb
.

<%=render partial: 'show' locals: { journal: @journal }%>

syntax error, unexpected tIDENTIFIER


In addition:
How can i make out @variable and variable,then do different things?

@journal = Journal.first
journal = Journal.first

if is @variable
do sth
else
do sth
end

Answer

You are missing a comma and this leads to a syntax error. Change

<%=render partial: 'show' locals: { journal: @journal }%>

to

<%= render partial: 'show', locals: { journal: @journal } %>

I addition: You can use the defined? method to check if a variable is defined, for example:

if defined?(@journal)
  # @journal is set
else
  # @journal is not set 
end