Forum: Ruby on Rails toggle! not working

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-11-10 21:11
I have a column in the database for called confirmed that is a
tinyint(1).

currently the value is set to 0.

i am trying to use the following function in my controller for
Compounds:

  def confirm
    @compound = Compound.find(params[:id])
    @compound.toggle!(:confirmed)
    redirect_to compound_path(@compound)
  end

i have my routes like the following:

  map.resources :compounds, :collection => { :calculator => :get },
:member => { :confirm => :post }

and this is what the link looks like:

http://domain/compounds/3960/confirm

but for some reason the value of confirmed is not changing.  anyone have
any ideas?
A1247f73598357f06388e863c41aa624?d=identicon&s=25 Elias Orozco (elioncho)
on 2008-11-10 22:06
(Received via mailing list)
Hi Scott,

Try :put instead of :post on the :member param, like this:

map.resources :compounds, :collection => { :calculator
=> :get }, :member => { :confirm => :put }
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2008-11-10 22:24
>   def confirm
>     @compound = Compound.find(params[:id])
>     @compound.toggle!(:confirmed)
>     redirect_to compound_path(@compound)
>   end

Shouldn't your confirm method always set the :confirmed flag to true
rather than toggling it anyway? Calling confirm on an already confirmed
item should remain confirmed right? In this case if confirm were called
on a confirmed item it would unconfirm it. It seems that should be a
separate operation to me.
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2008-11-10 22:38
Elias Orozco wrote:
> Hi Scott,
>
> Try :put instead of :post on the :member param, like this:
>
> map.resources :compounds, :collection => { :calculator
> => :get }, :member => { :confirm => :put }

Elias,

  i tried put but got this error back:

Only put requests are allowed.

robert,

  i think the toggle should toggle the value from 1->0 or 0->1 but i
can't even get it to toggle from 0->1 at the moment.
This topic is locked and can not be replied to.