I have this link in the view:


       <%= link_to "Pay in PayPal", @order.paypal_url(edit_order_url,
payment_notification_url), class: "btn btn-success" %>


And this in the model:


      class Order < ActiveRecord::Base
      def paypal_url(return_url, notify_url)
         # ... Do stuff
         self.update_attribute(:payment_in_process, true)
         # Do more stuff...
       end

When I visit the view, the model gets its attribute updated. I do not
want this. I only want the attribute updated if the link is clicked.

Know how to accomplish this?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/40a63abd3f759f1c0a3a39810161b0db%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Nanaya at Jun 12, 2016 at 7:13 am
    Hi
    On Sun, Jun 12, 2016, at 16:03, David Alejandro wrote:
    I have this link in the view:


    <%= link_to "Pay in PayPal", @order.paypal_url(edit_order_url,
    payment_notification_url), class: "btn btn-success" %>


    And this in the model:


    class Order < ActiveRecord::Base
    def paypal_url(return_url, notify_url)
    # ... Do stuff
    self.update_attribute(:payment_in_process, true)
    # Do more stuff...
    end

    When I visit the view, the model gets its attribute updated. I do not
    want this. I only want the attribute updated if the link is clicked.
    You call the method in the view, thus the method is run. Nothing more,
    nothing less.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1465715590.2988529.635088441.28CD17F5%40webmail.messagingengine.com.
    For more options, visit https://groups.google.com/d/optout.
  • David Alejandro at Jun 12, 2016 at 7:24 am

    nanaya wrote in post #1183942:
    Hi
    On Sun, Jun 12, 2016, at 16:03, David Alejandro wrote:
    class Order < ActiveRecord::Base
    def paypal_url(return_url, notify_url)
    # ... Do stuff
    self.update_attribute(:payment_in_process, true)
    # Do more stuff...
    end

    When I visit the view, the model gets its attribute updated. I do not
    want this. I only want the attribute updated if the link is clicked.
    You call the method in the view, thus the method is run. Nothing more,
    nothing less.
    Nanaya, what can I do to only update the attribute when the link is
    clicked?

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9459b1a8bcb06a31a041687bb12f70fd%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Jun 12, 2016 at 7:39 am

    On 12 June 2016 at 08:03, David Alejandro wrote:
    I have this link in the view:


    <%= link_to "Pay in PayPal", @order.paypal_url(edit_order_url,
    payment_notification_url), class: "btn btn-success" %>


    And this in the model:


    class Order < ActiveRecord::Base
    def paypal_url(return_url, notify_url)
    # ... Do stuff
    self.update_attribute(:payment_in_process, true)
    # Do more stuff...
    end

    When I visit the view, the model gets its attribute updated. I do not
    want this. I only want the attribute updated if the link is clicked.

    Know how to accomplish this?
    Separate out the code that generates the url from the code that
    updates the database. Then run the update in the action that is
    called when the link is clicked.

    Colin

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLspL8OBJXvsD%2BVjUhujnz28bBvH8eMjngSAB28mvBPNgQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • David Alejandro at Jun 12, 2016 at 7:55 am

    Colin Law wrote in post #1183944:
    On 12 June 2016 at 08:03, David Alejandro wrote:
    class Order < ActiveRecord::Base
    def paypal_url(return_url, notify_url)
    # ... Do stuff
    self.update_attribute(:payment_in_process, true)
    # Do more stuff...
    end

    When I visit the view, the model gets its attribute updated. I do not
    want this. I only want the attribute updated if the link is clicked.

    Know how to accomplish this?
    Separate out the code that generates the url from the code that
    updates the database. Then run the update in the action that is
    called when the link is clicked.

    Colin
    Colin, there's no action called when the link is clicked, it just
    follows the URL generated by the method.

    Tried separating the URL generator and creating an action but it turns
    out to be the same...

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d6110eb565deb6d871783a4cabe123d0%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Colin Law at Jun 12, 2016 at 8:51 am

    On 12 June 2016 at 08:55, David Alejandro wrote:
    Colin Law wrote in post #1183944:
    On 12 June 2016 at 08:03, David Alejandro wrote:
    class Order < ActiveRecord::Base
    def paypal_url(return_url, notify_url)
    # ... Do stuff
    self.update_attribute(:payment_in_process, true)
    # Do more stuff...
    end

    When I visit the view, the model gets its attribute updated. I do not
    want this. I only want the attribute updated if the link is clicked.

    Know how to accomplish this?
    Separate out the code that generates the url from the code that
    updates the database. Then run the update in the action that is
    called when the link is clicked.

    Colin
    Colin, there's no action called when the link is clicked, it just
    follows the URL generated by the method.
    Well that is what you need to fix. If you need to do something to the
    database then you need to call an action in your application that does
    whatever it is you want to do. If necessary you can then redirect to
    your external url.

    Are you a beginner with Rails? If so then I suggest that before going
    further you work right through a good tutorial such as
    railstutorial.org (which is free to use online), that will show you
    the basics of Rails.

    Colin
    Tried separating the URL generator and creating an action but it turns
    out to be the same...

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d6110eb565deb6d871783a4cabe123d0%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLv7b4xHnerMgWAR3%2Bvce_EXGWUjP%3DLx%2B1L%3D_GnKPS3stQ%40mail.gmail.com.
    For more options, visit https://groups.google.com/d/optout.
  • David Alejandro at Jun 12, 2016 at 12:21 pm

    Colin Law wrote in post #1183946:
    On 12 June 2016 at 08:55, David Alejandro wrote:
    want this. I only want the attribute updated if the link is clicked.
    follows the URL generated by the method.
    Well that is what you need to fix. If you need to do something to the
    database then you need to call an action in your application that does
    whatever it is you want to do. If necessary you can then redirect to
    your external url.

    Are you a beginner with Rails? If so then I suggest that before going
    further you work right through a good tutorial such as
    railstutorial.org (which is free to use online), that will show you
    the basics of Rails.

    Colin
    Managed to solve this a while ago :-)

    I created an action for the link which generates and follows the link,
    then when it gets back from it (PayPal), it updates the database.

       post 'orders/:id/pay', to: 'orders#start_payment_process', as:
    'start_payment_process'

         <%= link_to "Pagar en PayPal", start_payment_process_path, class:
    "btn btn-success", method: :post %>

       def start_payment_process
         @order = current_order
         @order.update_attribute(:payment_in_process, true)
         redirect_to @order.paypal_url(edit_order_url,
    payment_notification_url)
       end

       def edit
         # ... Some code
         if @order.payment_notifications.any? && @order.payment_in_process
           @order.update_attribute(:payment_in_process, false)
         end
       end

    Thanks!

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/891aa23f1ccf65931444f00b3478e7b4%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJun 12, '16 at 7:03a
activeJun 12, '16 at 12:21p
posts7
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase