开发者

Rails - updating a boolean value through a controller

UPDATE: Controller code at the bottom of this question resolves the issue. I'm struggling to update a boolean confirmed_flag field in rails 3. Background: users can host events and can attend events through the attendances model (HATM). I have a form_for helper which uses radio buttons to allow a guest to be accepted or declined from an event:

<%= form_for(@attendance) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
  <div class="field">
  <strong>Event description: </strong><%= @attendance.event.description %><br/>
<%= f.radio_button :confirmed_flag, 1 %>Accept
<%= f.radio_button :confirmed_flag, 0 %>Decline

This displays fine and hooks into the following controller update action:

def update
  @attendance = Attendance.find(params[:id]) 
  respond_to do |format|
    开发者_C百科if @attendance.update_attributes(params[:attendance][:confirmed_flag])
      format.html { redirect_to(@attendance,
                    :notice => 'Attendance was successfully updated.') }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @attendance.errors,
                    :status => :unprocessable_entity }
    end
  end
end

As you can see, i want the results from the form_for to update the confirmed_flag, but when i look at the values in the rails console for a particular attendance i have updated to 'Accept' in the form, the value of confirmed_flag remains nil.

Any help greatly appreciated.

Edit: relevant server log snippet attached:

Started POST "/attendances/10" for 127.0.0.1 at 2011-03-21 23:42:18 +0000
  Processing by AttendancesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Oz5qO/bcEJGmzIEH0xDOvtv7ecbF4wXXHpUtlIkDnfc=", "attendance"=>{"confirmed_flag"=>"1"}, "commit"=>"Update", "id"=>"10"}
  [1m[36mAttendance Load (15.6ms)[0m  [1mSELECT "attendances".* FROM "attendances" WHERE "attendances"."id" = 10 LIMIT 1[0m
Redirected to http://127.0.0.1:3000/attendances/10
Completed 302 Found in 109ms

Update: The following code in the controller works perfectly. Thanks everybody for your help as always.

def update
        @attendance = Attendance.find(params[:id]) 
        respond_to do |format|
    if 
        @attendance.update_attributes({:confirmed_flag => params[:attendance][:confirmed_flag]})
        format.html { redirect_to(@attendance,
                    :notice => 'Attendance was successfully updated.') }
        format.xml  { head :ok }
    else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @attendance.errors,
                    :status => :unprocessable_entity }
            end
        end
    end


From your comment to my other answer:

is there a way to use the update_attribute method (rather than update_attributes) and somehow pass in the value of the radio box to the singlular method?

You could do it manually:

@attendance.confirmed_flag = (params[:confirmed_flag] == "1")
@attendance.save


I think the conditional should say this.

@attendance.update_attributes({:confirmed_flag => params[:attendance][:confirmed_flag]})


I think it might work if you remove the [:confirmed_flag] part of your update statement.

So just:

if @attendance.update_attributes(params[:attendance])

I'm not 100% sure, but the update_attributes method might be looking for the attendance fields as nested within "confirmed_flag"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜