Rails: elsif && conditional statement in controller action not working





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am working on a multistep form, which is based on Ryan Bates' #217 Multistep Forms tutorial. I needed to implement conditional navigation inside the form and things became quite complex.



Navigation inside the form is working but I have problems with the conditions in my controller relative to the commit.



def create
@order.current_step = session[:order_step]
if @order.valid?
if params[:back_button]
@order.previous_step
elsif params[:back_button_wiretransfer]
@order.payment_options_step
elsif params[:back_button_credit_card]
@order.creditcard_options_step
elsif @order.thirdlast_step? && params[:commit] == 'Wiretransfer'
@order.payment = nil
@order.payment = 'Wiretransfer'
@order.confirmation_step
elsif @order.thirdlast_step? && params[:commit] == 'Credit card'
@order.next_step
elsif @order.secondlast_step?
@order.payment = nil
@order.payment = 'Credit card'
@order.next_step
elsif @order.last_step? && @order.payment = 'Wiretransfer'
...
elsif @order.last_step? && @order.payment = 'Credit card'
...
else
@order.next_step
end
session[:order_step] = @order.current_step
end
if @order.new_record?
render "new"
else
...
end
end


Currently the controller is not able to apply the last two elsif conditions with @order.payment = 'wiretransfer' or @order.payment = 'credit card'.



All other conditions are working, but when I checkout through the credit card section the wiretransfer part is executed.



When I use:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment = 'Credit card'
...


'Credit card' works, but 'wiretransfer' does not. Since I display the payment attribute on the last step of the form I know it persists at that step although it wasn't saved yet to the database.



What am I doing wrong? I found out that sometimes I have to use = and other times ==, but the rational is not clear to me.



In the view I have to use == (comparison) such as in:



<% elsif @order.last_step? && @order.payment == "Credit card" %>


to trigger the right divs.



In the controller the two conditions



elsif @order.thirdlast_step? && params[:commit] == 'Wiretransfer' 
elsif @order.thirdlast_step? && params[:commit] == 'Credit card'


work only with ==, while all the others only with =. I started by using only == in the controller, but when I do so nothing gets executed and new is rendered.



Thank you in advance!










share|improve this question




















  • 1





    Seeing methods like thirdlast_step? is kind of concerning. Perhaps there's a better way of expressing this, like steering it towards a case to break out step number and :commit parameter.

    – tadman
    Jan 4 at 15:50











  • Yes I am concerned indeed ;0). I didn't refactor yet but only added two steps to see if conditional navigation would work.

    – SEJU
    Jan 4 at 15:54











  • As a note, this tutorial does seem unnecessarily complicated and a fair bit out of date (Rails 3.2?) so take its advice with a grain of salt.

    – tadman
    Jan 4 at 15:55






  • 1





    Looking over this code I'm not sure this is the best approach. You might be better off saving a record that has some kind of "pending" or "incomplete" flag set and kicking that forward between each request, adding data as necessary. The final step flips that flag off, making it "real". This approach seems to be the snowball approach where parameters are accumulated in the views and kicked back and forth each time. That's often a lot messier than having a table with (potentially) incomplete records you need to keep out of scope and invisible until made real.

    – tadman
    Jan 4 at 16:03






  • 1





    Thanks for your advice which I will treasure. I changed the conditional to query the button and now it works. Since it was the last piece I wanted to get everything running first. Now I will start polishing + refactoring look into your suggestion more thoroughly. Thanks again!

    – SEJU
    Jan 4 at 18:15




















1















I am working on a multistep form, which is based on Ryan Bates' #217 Multistep Forms tutorial. I needed to implement conditional navigation inside the form and things became quite complex.



Navigation inside the form is working but I have problems with the conditions in my controller relative to the commit.



def create
@order.current_step = session[:order_step]
if @order.valid?
if params[:back_button]
@order.previous_step
elsif params[:back_button_wiretransfer]
@order.payment_options_step
elsif params[:back_button_credit_card]
@order.creditcard_options_step
elsif @order.thirdlast_step? && params[:commit] == 'Wiretransfer'
@order.payment = nil
@order.payment = 'Wiretransfer'
@order.confirmation_step
elsif @order.thirdlast_step? && params[:commit] == 'Credit card'
@order.next_step
elsif @order.secondlast_step?
@order.payment = nil
@order.payment = 'Credit card'
@order.next_step
elsif @order.last_step? && @order.payment = 'Wiretransfer'
...
elsif @order.last_step? && @order.payment = 'Credit card'
...
else
@order.next_step
end
session[:order_step] = @order.current_step
end
if @order.new_record?
render "new"
else
...
end
end


Currently the controller is not able to apply the last two elsif conditions with @order.payment = 'wiretransfer' or @order.payment = 'credit card'.



All other conditions are working, but when I checkout through the credit card section the wiretransfer part is executed.



When I use:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment = 'Credit card'
...


'Credit card' works, but 'wiretransfer' does not. Since I display the payment attribute on the last step of the form I know it persists at that step although it wasn't saved yet to the database.



What am I doing wrong? I found out that sometimes I have to use = and other times ==, but the rational is not clear to me.



In the view I have to use == (comparison) such as in:



<% elsif @order.last_step? && @order.payment == "Credit card" %>


to trigger the right divs.



In the controller the two conditions



elsif @order.thirdlast_step? && params[:commit] == 'Wiretransfer' 
elsif @order.thirdlast_step? && params[:commit] == 'Credit card'


work only with ==, while all the others only with =. I started by using only == in the controller, but when I do so nothing gets executed and new is rendered.



Thank you in advance!










share|improve this question




















  • 1





    Seeing methods like thirdlast_step? is kind of concerning. Perhaps there's a better way of expressing this, like steering it towards a case to break out step number and :commit parameter.

    – tadman
    Jan 4 at 15:50











  • Yes I am concerned indeed ;0). I didn't refactor yet but only added two steps to see if conditional navigation would work.

    – SEJU
    Jan 4 at 15:54











  • As a note, this tutorial does seem unnecessarily complicated and a fair bit out of date (Rails 3.2?) so take its advice with a grain of salt.

    – tadman
    Jan 4 at 15:55






  • 1





    Looking over this code I'm not sure this is the best approach. You might be better off saving a record that has some kind of "pending" or "incomplete" flag set and kicking that forward between each request, adding data as necessary. The final step flips that flag off, making it "real". This approach seems to be the snowball approach where parameters are accumulated in the views and kicked back and forth each time. That's often a lot messier than having a table with (potentially) incomplete records you need to keep out of scope and invisible until made real.

    – tadman
    Jan 4 at 16:03






  • 1





    Thanks for your advice which I will treasure. I changed the conditional to query the button and now it works. Since it was the last piece I wanted to get everything running first. Now I will start polishing + refactoring look into your suggestion more thoroughly. Thanks again!

    – SEJU
    Jan 4 at 18:15
















1












1








1








I am working on a multistep form, which is based on Ryan Bates' #217 Multistep Forms tutorial. I needed to implement conditional navigation inside the form and things became quite complex.



Navigation inside the form is working but I have problems with the conditions in my controller relative to the commit.



def create
@order.current_step = session[:order_step]
if @order.valid?
if params[:back_button]
@order.previous_step
elsif params[:back_button_wiretransfer]
@order.payment_options_step
elsif params[:back_button_credit_card]
@order.creditcard_options_step
elsif @order.thirdlast_step? && params[:commit] == 'Wiretransfer'
@order.payment = nil
@order.payment = 'Wiretransfer'
@order.confirmation_step
elsif @order.thirdlast_step? && params[:commit] == 'Credit card'
@order.next_step
elsif @order.secondlast_step?
@order.payment = nil
@order.payment = 'Credit card'
@order.next_step
elsif @order.last_step? && @order.payment = 'Wiretransfer'
...
elsif @order.last_step? && @order.payment = 'Credit card'
...
else
@order.next_step
end
session[:order_step] = @order.current_step
end
if @order.new_record?
render "new"
else
...
end
end


Currently the controller is not able to apply the last two elsif conditions with @order.payment = 'wiretransfer' or @order.payment = 'credit card'.



All other conditions are working, but when I checkout through the credit card section the wiretransfer part is executed.



When I use:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment = 'Credit card'
...


'Credit card' works, but 'wiretransfer' does not. Since I display the payment attribute on the last step of the form I know it persists at that step although it wasn't saved yet to the database.



What am I doing wrong? I found out that sometimes I have to use = and other times ==, but the rational is not clear to me.



In the view I have to use == (comparison) such as in:



<% elsif @order.last_step? && @order.payment == "Credit card" %>


to trigger the right divs.



In the controller the two conditions



elsif @order.thirdlast_step? && params[:commit] == 'Wiretransfer' 
elsif @order.thirdlast_step? && params[:commit] == 'Credit card'


work only with ==, while all the others only with =. I started by using only == in the controller, but when I do so nothing gets executed and new is rendered.



Thank you in advance!










share|improve this question
















I am working on a multistep form, which is based on Ryan Bates' #217 Multistep Forms tutorial. I needed to implement conditional navigation inside the form and things became quite complex.



Navigation inside the form is working but I have problems with the conditions in my controller relative to the commit.



def create
@order.current_step = session[:order_step]
if @order.valid?
if params[:back_button]
@order.previous_step
elsif params[:back_button_wiretransfer]
@order.payment_options_step
elsif params[:back_button_credit_card]
@order.creditcard_options_step
elsif @order.thirdlast_step? && params[:commit] == 'Wiretransfer'
@order.payment = nil
@order.payment = 'Wiretransfer'
@order.confirmation_step
elsif @order.thirdlast_step? && params[:commit] == 'Credit card'
@order.next_step
elsif @order.secondlast_step?
@order.payment = nil
@order.payment = 'Credit card'
@order.next_step
elsif @order.last_step? && @order.payment = 'Wiretransfer'
...
elsif @order.last_step? && @order.payment = 'Credit card'
...
else
@order.next_step
end
session[:order_step] = @order.current_step
end
if @order.new_record?
render "new"
else
...
end
end


Currently the controller is not able to apply the last two elsif conditions with @order.payment = 'wiretransfer' or @order.payment = 'credit card'.



All other conditions are working, but when I checkout through the credit card section the wiretransfer part is executed.



When I use:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment = 'Credit card'
...


'Credit card' works, but 'wiretransfer' does not. Since I display the payment attribute on the last step of the form I know it persists at that step although it wasn't saved yet to the database.



What am I doing wrong? I found out that sometimes I have to use = and other times ==, but the rational is not clear to me.



In the view I have to use == (comparison) such as in:



<% elsif @order.last_step? && @order.payment == "Credit card" %>


to trigger the right divs.



In the controller the two conditions



elsif @order.thirdlast_step? && params[:commit] == 'Wiretransfer' 
elsif @order.thirdlast_step? && params[:commit] == 'Credit card'


work only with ==, while all the others only with =. I started by using only == in the controller, but when I do so nothing gets executed and new is rendered.



Thank you in advance!







ruby-on-rails ruby ruby-on-rails-3 conditional






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 16:00







SEJU

















asked Jan 4 at 15:47









SEJUSEJU

240213




240213








  • 1





    Seeing methods like thirdlast_step? is kind of concerning. Perhaps there's a better way of expressing this, like steering it towards a case to break out step number and :commit parameter.

    – tadman
    Jan 4 at 15:50











  • Yes I am concerned indeed ;0). I didn't refactor yet but only added two steps to see if conditional navigation would work.

    – SEJU
    Jan 4 at 15:54











  • As a note, this tutorial does seem unnecessarily complicated and a fair bit out of date (Rails 3.2?) so take its advice with a grain of salt.

    – tadman
    Jan 4 at 15:55






  • 1





    Looking over this code I'm not sure this is the best approach. You might be better off saving a record that has some kind of "pending" or "incomplete" flag set and kicking that forward between each request, adding data as necessary. The final step flips that flag off, making it "real". This approach seems to be the snowball approach where parameters are accumulated in the views and kicked back and forth each time. That's often a lot messier than having a table with (potentially) incomplete records you need to keep out of scope and invisible until made real.

    – tadman
    Jan 4 at 16:03






  • 1





    Thanks for your advice which I will treasure. I changed the conditional to query the button and now it works. Since it was the last piece I wanted to get everything running first. Now I will start polishing + refactoring look into your suggestion more thoroughly. Thanks again!

    – SEJU
    Jan 4 at 18:15
















  • 1





    Seeing methods like thirdlast_step? is kind of concerning. Perhaps there's a better way of expressing this, like steering it towards a case to break out step number and :commit parameter.

    – tadman
    Jan 4 at 15:50











  • Yes I am concerned indeed ;0). I didn't refactor yet but only added two steps to see if conditional navigation would work.

    – SEJU
    Jan 4 at 15:54











  • As a note, this tutorial does seem unnecessarily complicated and a fair bit out of date (Rails 3.2?) so take its advice with a grain of salt.

    – tadman
    Jan 4 at 15:55






  • 1





    Looking over this code I'm not sure this is the best approach. You might be better off saving a record that has some kind of "pending" or "incomplete" flag set and kicking that forward between each request, adding data as necessary. The final step flips that flag off, making it "real". This approach seems to be the snowball approach where parameters are accumulated in the views and kicked back and forth each time. That's often a lot messier than having a table with (potentially) incomplete records you need to keep out of scope and invisible until made real.

    – tadman
    Jan 4 at 16:03






  • 1





    Thanks for your advice which I will treasure. I changed the conditional to query the button and now it works. Since it was the last piece I wanted to get everything running first. Now I will start polishing + refactoring look into your suggestion more thoroughly. Thanks again!

    – SEJU
    Jan 4 at 18:15










1




1





Seeing methods like thirdlast_step? is kind of concerning. Perhaps there's a better way of expressing this, like steering it towards a case to break out step number and :commit parameter.

– tadman
Jan 4 at 15:50





Seeing methods like thirdlast_step? is kind of concerning. Perhaps there's a better way of expressing this, like steering it towards a case to break out step number and :commit parameter.

– tadman
Jan 4 at 15:50













Yes I am concerned indeed ;0). I didn't refactor yet but only added two steps to see if conditional navigation would work.

– SEJU
Jan 4 at 15:54





Yes I am concerned indeed ;0). I didn't refactor yet but only added two steps to see if conditional navigation would work.

– SEJU
Jan 4 at 15:54













As a note, this tutorial does seem unnecessarily complicated and a fair bit out of date (Rails 3.2?) so take its advice with a grain of salt.

– tadman
Jan 4 at 15:55





As a note, this tutorial does seem unnecessarily complicated and a fair bit out of date (Rails 3.2?) so take its advice with a grain of salt.

– tadman
Jan 4 at 15:55




1




1





Looking over this code I'm not sure this is the best approach. You might be better off saving a record that has some kind of "pending" or "incomplete" flag set and kicking that forward between each request, adding data as necessary. The final step flips that flag off, making it "real". This approach seems to be the snowball approach where parameters are accumulated in the views and kicked back and forth each time. That's often a lot messier than having a table with (potentially) incomplete records you need to keep out of scope and invisible until made real.

– tadman
Jan 4 at 16:03





Looking over this code I'm not sure this is the best approach. You might be better off saving a record that has some kind of "pending" or "incomplete" flag set and kicking that forward between each request, adding data as necessary. The final step flips that flag off, making it "real". This approach seems to be the snowball approach where parameters are accumulated in the views and kicked back and forth each time. That's often a lot messier than having a table with (potentially) incomplete records you need to keep out of scope and invisible until made real.

– tadman
Jan 4 at 16:03




1




1





Thanks for your advice which I will treasure. I changed the conditional to query the button and now it works. Since it was the last piece I wanted to get everything running first. Now I will start polishing + refactoring look into your suggestion more thoroughly. Thanks again!

– SEJU
Jan 4 at 18:15







Thanks for your advice which I will treasure. I changed the conditional to query the button and now it works. Since it was the last piece I wanted to get everything running first. Now I will start polishing + refactoring look into your suggestion more thoroughly. Thanks again!

– SEJU
Jan 4 at 18:15














1 Answer
1






active

oldest

votes


















4














You should use == in both cases:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment == 'Credit card'
...


= assigns a value to a variable, whereas == checks if two values/variables are equal.



As = returns an assigned value, that is not nil in those two cases it's evaluated as the truth - that's why when you have:



elsif @order.last_step? && @order.payment = 'Wiretransfer'


the next elsif is never called.



Worth reading:




  • http://ruby.bastardsbook.com/chapters/ifelse/#h-2-2

  • https://skorks.com/2009/09/true-false-and-nil-objects-in-ruby/






share|improve this answer





















  • 2





    This difference might seem minor, but programmers often need to develop an eye for this particular detail as the difference is huge.

    – tadman
    Jan 4 at 15:51











  • Thank you for your feedback. Indeed I started with == but somehow the second part of the condition is not executed and a new view is rendered.

    – SEJU
    Jan 4 at 16:16






  • 2





    && uses something called "short-circuit evaluation", meaning that if the left part of the expression returns false, there's no need to evaluate the right part of the expression. I would make sure @order.last_step? is true, otherwise @order.payment will never be checked.

    – XML Slayer
    Jan 4 at 16:41













  • Thanks for your reply! In my last view the same conditional elsif @order.last_step? && @order.payment == "Wiretransfer" does work. Only the controller statement somehow does not work ...

    – SEJU
    Jan 4 at 16:54











  • Thanks everyone I learned a lot! I had to change the statement and use a params[:somebutton] now it works and I can start refactoring and improving. I will accept the answer ... although it did not solve my problem completely, it pointed me into the right direction decisively and is the most sensible thing to say about this chaos ;0). I think that somehow it is not possible to query inside a conditional statement of a controller action for an attribute created in the same action but not yet written to the DB...

    – SEJU
    Jan 4 at 18:27














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042137%2frails-elsif-conditional-statement-in-controller-action-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You should use == in both cases:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment == 'Credit card'
...


= assigns a value to a variable, whereas == checks if two values/variables are equal.



As = returns an assigned value, that is not nil in those two cases it's evaluated as the truth - that's why when you have:



elsif @order.last_step? && @order.payment = 'Wiretransfer'


the next elsif is never called.



Worth reading:




  • http://ruby.bastardsbook.com/chapters/ifelse/#h-2-2

  • https://skorks.com/2009/09/true-false-and-nil-objects-in-ruby/






share|improve this answer





















  • 2





    This difference might seem minor, but programmers often need to develop an eye for this particular detail as the difference is huge.

    – tadman
    Jan 4 at 15:51











  • Thank you for your feedback. Indeed I started with == but somehow the second part of the condition is not executed and a new view is rendered.

    – SEJU
    Jan 4 at 16:16






  • 2





    && uses something called "short-circuit evaluation", meaning that if the left part of the expression returns false, there's no need to evaluate the right part of the expression. I would make sure @order.last_step? is true, otherwise @order.payment will never be checked.

    – XML Slayer
    Jan 4 at 16:41













  • Thanks for your reply! In my last view the same conditional elsif @order.last_step? && @order.payment == "Wiretransfer" does work. Only the controller statement somehow does not work ...

    – SEJU
    Jan 4 at 16:54











  • Thanks everyone I learned a lot! I had to change the statement and use a params[:somebutton] now it works and I can start refactoring and improving. I will accept the answer ... although it did not solve my problem completely, it pointed me into the right direction decisively and is the most sensible thing to say about this chaos ;0). I think that somehow it is not possible to query inside a conditional statement of a controller action for an attribute created in the same action but not yet written to the DB...

    – SEJU
    Jan 4 at 18:27


















4














You should use == in both cases:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment == 'Credit card'
...


= assigns a value to a variable, whereas == checks if two values/variables are equal.



As = returns an assigned value, that is not nil in those two cases it's evaluated as the truth - that's why when you have:



elsif @order.last_step? && @order.payment = 'Wiretransfer'


the next elsif is never called.



Worth reading:




  • http://ruby.bastardsbook.com/chapters/ifelse/#h-2-2

  • https://skorks.com/2009/09/true-false-and-nil-objects-in-ruby/






share|improve this answer





















  • 2





    This difference might seem minor, but programmers often need to develop an eye for this particular detail as the difference is huge.

    – tadman
    Jan 4 at 15:51











  • Thank you for your feedback. Indeed I started with == but somehow the second part of the condition is not executed and a new view is rendered.

    – SEJU
    Jan 4 at 16:16






  • 2





    && uses something called "short-circuit evaluation", meaning that if the left part of the expression returns false, there's no need to evaluate the right part of the expression. I would make sure @order.last_step? is true, otherwise @order.payment will never be checked.

    – XML Slayer
    Jan 4 at 16:41













  • Thanks for your reply! In my last view the same conditional elsif @order.last_step? && @order.payment == "Wiretransfer" does work. Only the controller statement somehow does not work ...

    – SEJU
    Jan 4 at 16:54











  • Thanks everyone I learned a lot! I had to change the statement and use a params[:somebutton] now it works and I can start refactoring and improving. I will accept the answer ... although it did not solve my problem completely, it pointed me into the right direction decisively and is the most sensible thing to say about this chaos ;0). I think that somehow it is not possible to query inside a conditional statement of a controller action for an attribute created in the same action but not yet written to the DB...

    – SEJU
    Jan 4 at 18:27
















4












4








4







You should use == in both cases:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment == 'Credit card'
...


= assigns a value to a variable, whereas == checks if two values/variables are equal.



As = returns an assigned value, that is not nil in those two cases it's evaluated as the truth - that's why when you have:



elsif @order.last_step? && @order.payment = 'Wiretransfer'


the next elsif is never called.



Worth reading:




  • http://ruby.bastardsbook.com/chapters/ifelse/#h-2-2

  • https://skorks.com/2009/09/true-false-and-nil-objects-in-ruby/






share|improve this answer















You should use == in both cases:



elsif @order.last_step? && @order.payment == 'Wiretransfer'
...
elsif @order.last_step? && @order.payment == 'Credit card'
...


= assigns a value to a variable, whereas == checks if two values/variables are equal.



As = returns an assigned value, that is not nil in those two cases it's evaluated as the truth - that's why when you have:



elsif @order.last_step? && @order.payment = 'Wiretransfer'


the next elsif is never called.



Worth reading:




  • http://ruby.bastardsbook.com/chapters/ifelse/#h-2-2

  • https://skorks.com/2009/09/true-false-and-nil-objects-in-ruby/







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 15:53

























answered Jan 4 at 15:50









mrzasamrzasa

10.7k104079




10.7k104079








  • 2





    This difference might seem minor, but programmers often need to develop an eye for this particular detail as the difference is huge.

    – tadman
    Jan 4 at 15:51











  • Thank you for your feedback. Indeed I started with == but somehow the second part of the condition is not executed and a new view is rendered.

    – SEJU
    Jan 4 at 16:16






  • 2





    && uses something called "short-circuit evaluation", meaning that if the left part of the expression returns false, there's no need to evaluate the right part of the expression. I would make sure @order.last_step? is true, otherwise @order.payment will never be checked.

    – XML Slayer
    Jan 4 at 16:41













  • Thanks for your reply! In my last view the same conditional elsif @order.last_step? && @order.payment == "Wiretransfer" does work. Only the controller statement somehow does not work ...

    – SEJU
    Jan 4 at 16:54











  • Thanks everyone I learned a lot! I had to change the statement and use a params[:somebutton] now it works and I can start refactoring and improving. I will accept the answer ... although it did not solve my problem completely, it pointed me into the right direction decisively and is the most sensible thing to say about this chaos ;0). I think that somehow it is not possible to query inside a conditional statement of a controller action for an attribute created in the same action but not yet written to the DB...

    – SEJU
    Jan 4 at 18:27
















  • 2





    This difference might seem minor, but programmers often need to develop an eye for this particular detail as the difference is huge.

    – tadman
    Jan 4 at 15:51











  • Thank you for your feedback. Indeed I started with == but somehow the second part of the condition is not executed and a new view is rendered.

    – SEJU
    Jan 4 at 16:16






  • 2





    && uses something called "short-circuit evaluation", meaning that if the left part of the expression returns false, there's no need to evaluate the right part of the expression. I would make sure @order.last_step? is true, otherwise @order.payment will never be checked.

    – XML Slayer
    Jan 4 at 16:41













  • Thanks for your reply! In my last view the same conditional elsif @order.last_step? && @order.payment == "Wiretransfer" does work. Only the controller statement somehow does not work ...

    – SEJU
    Jan 4 at 16:54











  • Thanks everyone I learned a lot! I had to change the statement and use a params[:somebutton] now it works and I can start refactoring and improving. I will accept the answer ... although it did not solve my problem completely, it pointed me into the right direction decisively and is the most sensible thing to say about this chaos ;0). I think that somehow it is not possible to query inside a conditional statement of a controller action for an attribute created in the same action but not yet written to the DB...

    – SEJU
    Jan 4 at 18:27










2




2





This difference might seem minor, but programmers often need to develop an eye for this particular detail as the difference is huge.

– tadman
Jan 4 at 15:51





This difference might seem minor, but programmers often need to develop an eye for this particular detail as the difference is huge.

– tadman
Jan 4 at 15:51













Thank you for your feedback. Indeed I started with == but somehow the second part of the condition is not executed and a new view is rendered.

– SEJU
Jan 4 at 16:16





Thank you for your feedback. Indeed I started with == but somehow the second part of the condition is not executed and a new view is rendered.

– SEJU
Jan 4 at 16:16




2




2





&& uses something called "short-circuit evaluation", meaning that if the left part of the expression returns false, there's no need to evaluate the right part of the expression. I would make sure @order.last_step? is true, otherwise @order.payment will never be checked.

– XML Slayer
Jan 4 at 16:41







&& uses something called "short-circuit evaluation", meaning that if the left part of the expression returns false, there's no need to evaluate the right part of the expression. I would make sure @order.last_step? is true, otherwise @order.payment will never be checked.

– XML Slayer
Jan 4 at 16:41















Thanks for your reply! In my last view the same conditional elsif @order.last_step? && @order.payment == "Wiretransfer" does work. Only the controller statement somehow does not work ...

– SEJU
Jan 4 at 16:54





Thanks for your reply! In my last view the same conditional elsif @order.last_step? && @order.payment == "Wiretransfer" does work. Only the controller statement somehow does not work ...

– SEJU
Jan 4 at 16:54













Thanks everyone I learned a lot! I had to change the statement and use a params[:somebutton] now it works and I can start refactoring and improving. I will accept the answer ... although it did not solve my problem completely, it pointed me into the right direction decisively and is the most sensible thing to say about this chaos ;0). I think that somehow it is not possible to query inside a conditional statement of a controller action for an attribute created in the same action but not yet written to the DB...

– SEJU
Jan 4 at 18:27







Thanks everyone I learned a lot! I had to change the statement and use a params[:somebutton] now it works and I can start refactoring and improving. I will accept the answer ... although it did not solve my problem completely, it pointed me into the right direction decisively and is the most sensible thing to say about this chaos ;0). I think that somehow it is not possible to query inside a conditional statement of a controller action for an attribute created in the same action but not yet written to the DB...

– SEJU
Jan 4 at 18:27






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042137%2frails-elsif-conditional-statement-in-controller-action-not-working%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas