Ruby on Rails checkbox not updating on form submission

I have a Records Controller that allows users to add contact information to a website. User submitted information is not displayed to users until the administrator checks the checkbox and submits the form. So basically my problem is that if I check the box as administrator and create a record first (records # new), the record will be public as expected, but if a non-admin user creates a record (regular user view doesn 't enable the' live 'checkbox, only admin does) then this entry is stuck in limbo because # edit view entries for some reason do not update the boolean checkbox value when logged in as admin.

entries # new view:

<% form_for(@entry) do |f| %>
  <%= f.error_messages %>
  Name<br />
  <%= f.text_field :name %>

  Mailing Address<br />
  <%= f.text_field :address %>

  #...
  <%- if current_user -%>
    <%= f.label :live %><br />
    <%= f.check_box :live %>
  <%- end -%>
  <%= f.submit 'Create' %>
<% end %>

      

entries # edit (available for admin only):

<% form_for(@entry) do |f| %>
  <%= f.error_messages %>

  <%= f.label :name %><br />
  <%= f.text_field :name %>

  Mailing Address<br />
  <%= f.text_field :address %>

  <%= f.label :live %><br />
  <%= f.check_box :live %>

  <%= f.submit 'Update' %>

<% end %>

      

EDIT:

entries_controller.rb update method:

def update
  @entry = Entry.find(params[:id])
  respond_to do |format|
    if @entry.update_attributes(params[:entry])
      flash[:notice] = 'Entry was updated.'
      format.html { redirect_to(@entry) }
    else
      format.html { render :action => "edit" }
    end
  end
end

      

entry.rb:

class Entry < ActiveRecord::Base
  acts_as_mappable
  acts_as_taggable_on   :tags
  validates_presence_of :name, :tag_list
  validates_length_of   :name, :maximum => 64
  validates_length_of   :tag_list, :maximum => 128, :allow_blank => false
  validates_length_of   :paddress, :maximum => 128, :allow_blank => true
  validates_length_of   :address, :maximum => 128, :allow_blank => true
  validates_length_of   :tollfreephone, :in => 7..32, :allow_blank => true
  validates_length_of   :phone, :in => 7..32, :allow_blank => true
  validates_length_of   :phone2, :in => 7..32, :allow_blank => true
  validates_length_of   :mobile, :in => 7..32, :allow_blank => true
  validates_length_of   :fax, :in => 7..32, :allow_blank => true
  validates_length_of   :email, :in => 7..48, :allow_blank => true
  validates_format_of   :email,
                    :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
                    :on => :create, :allow_blank => true

  validates_length_of   :website, :maximum => 64, :allow_blank => true
  validates_length_of   :description, :maximum => 1024, :allow_blank => true
  attr_accessible       :name, :tag_list, :paddress, :address, :tollfreephone,
                        :phone, :phone2, :mobile, :fax, :email, :website,
                        :description 
  validate              :required_info
  before_save           :geocode_paddress
  searchable_on         :name, :address, :phone, :phone2, :mobile, :fax, :email,
                    :website, :category, :description
private
  def required_info
    if( phone.empty?  and phone2.empty? and tollfreephone.empty? and 
        mobile.empty? and fax.empty?    and email.empty?         and 
        website.empty? 
      ) 
      errors.add_to_base "Please have at least one form of contact information."
    end
  end
  def geocode_paddress
    # if paddress is nil or empty set the old values to nil and return    
    ((self.lat = self.lng = nil); return true) if paddress.empty?
    g=Geokit::Geocoders::MultiGeocoder.geocode(paddress)
    (errors.add(:paddress,"Could not Geocode address");
       return false) unless g.success
    self.lat, self.lng = g.lat, g.lng
  end
end

      

Any ideas as to why the admin can't update the: live checkbox from the edit window?

I would really appreciate any suggestions. I am new to rails. I can post more code if needed. Thanks for reading my question.

+2


a source to share


1 answer


You have attr_accessible call

attr_accessible       :name, :tag_list, :paddress, :address, :tollfreephone,
                    :phone, :phone2, :mobile, :fax, :email, :website,
                    :description 

      



This is great because it will prevent users from updating fields other than what you allow them. But your field live

is not there.

So this value is filtered by rails, since the user is not allowed to update this field. Add it to the fields in the call to attr_accessible and you can edit it.

+2


a source







All Articles