views:

149

answers:

1

I have an entries controller that allows users to add contact information the website. The user-submitted information isn't visible to users until the administrator checks a check box and submits the form. So basically my problem is that if I check the check box as an administrator while initially creating an entry (entries#new) the entry will be publicly visible as expected, but if a non-admin user creates an entry (the normal user view doesn't include the 'live' check box, only the admin one does) then that entry is stuck in limbo because the entries#edit view for some reason doesn't update the boolean check box value when logged in as an 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 (only accessible by admin) view:

<% 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 an administrator can't update the :live check box from the edit view?

I would greatly appreciate any suggestions. I'm new to rails. I can post more code if it's needed. Thanks for reading my question.

+2  A: 

You're having an attr_accessible call

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

Which is great as it'll avoid users to update other fields than the ones you allow them to.
But your live field isn't in there.

So it's value is filtered by rails as the user isn't allow to update that field. Add it to the fields in the attr_accessible call and you'll be able to edit it.

Damien MATHIEU
Thank you so much! I completely missed that.
I would recommend not adding it, that attribute should not be accessible, because then users could set it themselves. It would defeat the whole purpose of attr_accessible. Instead, do `@entry.live = params[:entry][:live]` before the call to update_attributes.
mckeed