Rails - using true and false

My controller has the following:

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

Then later in the create.je.erb file do:

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

This only works when @archived_new_status is true, when it is false, which does not seem to get false. When I do Rails.logger.info on @archived_new_status for false, it does not output anything, if I check it, I get false.

Any thoughts?

+3
source share
6 answers

The code in the controller can be written:

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

View: I don’t understand why you are checking if the variable is nil; @archive_new_statusis either true or false, so just call the JS code (using to_jsonfor any argument you have):

xxx.myfuncthatNeedsTrueOrFalse(<%= @archived_new_status.to_json %>);
+3
source

to try:

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

:

if params[:archive] && !params[:archive].blank?
    @archived_new_status = params[:archive]
end

. . Rails

0

, - @archived_new_status ? , , , true, false.

:

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

, , (, , ). :

@archived_new_status = params[:archive] == 'true' ? true : false

( , ). , , @archived_new_status ! , , . true, false. , :

   xxx.myfuncthatNeedsTrueOrFalse(<%= @archived_new_status.to_s %>);

( , javascript , true, false. , ? → .to_s )

, , , :

  • , @archived_new_status , ,
  • , , ? , ?
0

Rails.logger.info false (. ), , .

ERB <%= something %> something.to_s, false.to_s == 'false', , , , @archived_new_status - .

0

Try it. I accept this as @archived_new_status is boolean (true or false)

params[:archive] ? @archived_new_status : !@archived_new_status
0
source

Source: https://habr.com/ru/post/1786216/


All Articles