Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Using true and false

I have the following in my controller:

if params[:archive] == 'true'
  @archived_new_status = true
else
  @archived_new_status = false
end

Then later in create.je.erb do:

<% if !@archived_new_status.nil? %>
   xxx.myfuncthatNeedsTrueOrFalse(<%=@archived_new_status%>);
<% end %>

That only works when @archived_new_status is true, when it's false that seems to not be getting set as false. When I do a Rails.logger.info on @archived_new_status for false it outputs nothing, if I inspect it, I get false.

Any thoughts?

like image 804
AnApprentice Avatar asked Dec 01 '25 15:12

AnApprentice


2 Answers

The code in the controller can be written:

@archive_new_status = (params[:archive] == 'true')

View: I don't understand why you check if the variable is nil; @archive_new_status is either true or false, so simply call the JS code (using to_json for any argument you have):

xxx.myfuncthatNeedsTrueOrFalse(<%= @archived_new_status.to_json %>);
like image 121
tokland Avatar answered Dec 03 '25 07:12

tokland


try:

<% if !@archived_new_status.blank? %>
   xxx.myfuncthatNeedsTrueOrFalse(<%=@archived_new_status%>);
<% end %>
like image 37
mark Avatar answered Dec 03 '25 08:12

mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!