开发者

In Rails 3 how do I use button_to to change the value of a boolean?

开发者 https://www.devze.com 2023-03-29 14:21 出处:网络
I\'m trying to write a button_to statement that updates a boolean value in my activerecord database. Here\'s what I tried:

I'm trying to write a button_to statement that updates a boolean value in my activerecord database. Here's what I tried:

<%= button_to "To Amazon", :controller => 'payments', :true => @payment.to_amazon, :method => :put %>

Bigger picture what I'm trying to do is have a button that updates a payment object and triggers a c开发者_Python百科all to a private method for the gem remit to communicate with amazon payments. So:

1) How do I use button_to to update a boolean?

2) Is changing a boolean a good way to access a private method in the controller?


Well what you can do is use AJAX to make a callback to your controller to switch that boolean. In this case I would maybe just use a link or a checkbox instead, but in the end not a huge deal. If you want some code for an example on how to do this, let me know.

There was a similar question about specifying actions that I answered, and I used a link_to there like this:

link_to "Profile", :controller => "profiles", :action => "show", :method => :get, :id => @profile

So you can specify the action, but that only works with a link and not a button. But you could make some special action that did the update.

As for your secondary question, if I understand correctly, you want to set a boolean on the @payment object then after call a private method? I assume you store the boolean in the database as well. So therefore your controller action could do something like this:

@payment.call_amazon_payment_method

Then in payment.rb (assuming your boolean column is just called "boolean_col" for lack of a better fake name):

def call_amazon_payment_method
    new_val = self.boolean_col? false : true
    self.update_attributes(:boolean_col => new_val)
    # if that alone will trigger your private method, you're done, or:
    self.private_amazon_payment_method
end

I hope I've properly understood your question and have helped moved you towards an elegant solution.

0

精彩评论

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

关注公众号