开发者

Rails - Using true and false

开发者 https://www.devze.com 2023-02-05 03:34 出处:网络
I have the following in my controller: if params[:archive] == \'true\' @archived_new_status = true else @archived_new_status = 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 开发者_如何学C!@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?


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 %>);


try:

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


You could do:

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

But you may want to put validation in the model or raise application exceptions. See Rails form validation


Rails.logger.info false incorrectly outputs nothing (see my answer to your other question), so I think that's hiding whatever your real problem is.

ERB <%= something %> implicitly calls something.to_s, but false.to_s == 'false', so the most likely explanation for what you're seeing is that @archived_new_status is somehow not getting set at all, or not getting passed to the view template.


Try this. I am taking it as @archived_new_status is a Boolean (true or false)

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


Do I understand correctly that somehow the variable @archived_new_status is not set? Although, the code you show always sets it to either true or false.

I would rewrite as follows:

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

The same as the code above, only somewhat simpler (and maybe clearer). Then you could write it even shorter:

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

(actually this is completely identical to what you showed us). In your view, you can then be sure that @archived_new_status is always set! From the code you showed us, there is no other way. It is either true or false. So your view becomes:

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

(i presume you are calling some javascript function here, with a wanted parameter of either true or false. Are you sure that the value is converted correctly to a string? --> i added .to_s for clarity)

From what you show us, nothing much could be wrong, so to wrap up:

  • can you make sure that @archived_new_status is set correctly, if not, show the complete code sample
  • in the view you are rendering, are you converting the value correctly to a string? can you explain what you are trying to do?
0

精彩评论

暂无评论...
验证码 换一张
取 消