rails polymorphic Association doesn't save id -
this weird , don't it.
i got polymorphic new form storing address, create method doesn't create association.
location/new
%= form_for [@mappable, @location] |form| %> <div class="inline-label-container"> <%= form.label :street_address, "street address", :class => "inline" %> <%= form.text_field :street_address, :onkeyup => 'timed_input(this)', :value => current_user.location.address, :autocomplete => :off %> <%= form.label :location_description, "found location"%> <%= form.text_field :address, :value => @user.location.address, :disabled => :true%> <%= form.hidden_field :google_address, :value => @user.location.address %> <%= form.hidden_field :latitude, :value => @user.location.latitude %> <%= form.hidden_field :longitude, :value => @user.location.longitude %> <%= form.submit "enter location", :class => "btn btn-large btn-success" %> can drag , drop marker! <small> </small> </div> <%end%>
location_controller.rb
def new @user = current_user @mappable = load_mappable @location = @mappable.build_location end def create @user = current_user @mappable = load_mappable @location = @mappable.build_location(params[:location], :mappable_id => @mappable.id, :mappable_type => @mappable.class.base_class.name) respond_to |format| if @location.save format.html { redirect_to :back, notice: "#{@mappable.id}, #{@mappable.class.base_class.name}"} format.json { render :json => :back, :status => :created, :location => @user } #redirect_to @commentable, notice: "comment created." else format.html { redirect_to root_path } format.json { render :json => @location.errors, :status => :unprocessable_entity } #render :new end end def load_mappable resource, id = request.path.split('/')[1,2] @mappable = resource.singularize.classify.constantize.find(id)
end
in create method passing mappable_id , type last attempts them. odd because in after create notice correct mappable_id , mappable_type displayed
2.1.1 :085 > location.last location load (0.6ms) select "locations".* "locations" order "locations"."id" desc limit 1 => #<location id: 95, address: nil, longitude: -74.009457, latitude: 40.704513, postcode: nil, created_at: "2014-06-01 19:06:19", updated_at: "2014-06-01 19:06:20", actor_id: nil, country: nil, country_code: nil, postal_code: nil, city: nil, political: nil, loc ality: nil, sublocality: nil, street_address: "108 pearl street", google_address: "108 pearl street, new york, ny 10004, usa", ward _id: nil, **mappable_id: nil**, mappable_type: "skill">
why mappable_id missing ? please
location.rb
class location < activerecord::base include activemodel::validations belongs_to :mappable, :polymorphic => true searchkick has_one :map, :dependent => :destroy has_one :group, :through => :map, :source => :mappable, :source_type => 'group' belongs_to :ward accepts_nested_attributes_for :ward has_one :localfeed attr_accessible :address, :latitude, :longitude, :postcode, :user_id, :actor_id, :group_id, :country, :country_code, :postal_code, :city, :political, :locality, :sublocality, :street_address, :google_address, :location_attributes, :mappable_id, :mappable_type, :ward_id geocoded_by :to_s #before_save :geolocate validate :street_address, :address_validator => true after_validation :geocode #before_save :geolocate after_save :createward #after_save :ward_present #creates new ward , feed if not existant def address_changed? attrs = %w(street_address city postal_code) attrs.any?{|a| send "#{a}_changed?"} end def to_s "#{google_address} #{city} #{postal_code}" +" gb" end def gmaps4rails_address address end def ward_present if self.ward_id = nil? w = ward.find_by_name(self.city) self.ward_id = w.id else self.destroy end end geocoded_by :to_s |prof,results| if result = results.select{|res| res.country_code == "gb" }.first unless (result.latitude.nil? || result.longitude.nil?) prof.latitude = result.latitude prof.longitude = result.longitude prof.country = result.country prof.postal_code = result.postal_code prof.city = result.city #prof.political = result.political # prof.locality = result.locality # prof.sublocality = result.sublocality prof.street_address = result.street_address end result.coordinates end end def geolocate res = googlegeocoder.geocode(to_s) if res.success self.latitude = res.latitude self.lonitude = res.longitude else errors[:base] << "geocoding failed, check address" return false end end def createward unless self.city.nil? w = ward.find_by_name(self.city) if w.nil? c = self.create_ward!({:name => self.city, :city => self.city}) # should [name => self.sublocality] self.ward_id = c.id else self.ward_id = w.id end end end def self.groups self.where(:user_id => nil) end def self.users self.where(:group_id => nil) end def self.users_city(city) self.where(:mappable_type => "user", :city => city).map(&:mappable_id).uniq #returns array of user_ids! end def search_and_fill_latlng(address=nil) okresponse = false geocoder = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" if address == nil address = self.address end if address != nil && address != "" url = uri.escape(geocoder+address) resp = restclient.get(url) result = json.parse(resp.body) if result["status"] == "ok" self.latitude = result["results"][0]["geometry"]["location"]["lat"] self.longitude = result["results"][0]["geometry"]["location"]["lng"] okresponse = true end end okresponse end end
skill.rb
class skill < activerecord::base attr_accessible :avatar_attributes, :photo_attributes, :name, :description,:skill_type_id, :properties, :teachers_title, :necessary_resources, :level, :required_experience, :price, :start_date, :min_students, :max_students, :activity_duration, :location_id, :user_id, :location_attributes, :skill_type_attributes belongs_to :user belongs_to :skill_type has_one :ward, :as => :warded accepts_nested_attributes_for :skill_type has_one :location, :as => :mappable accepts_nested_attributes_for :location
included both model classes location , skill.
console
started post "/skills/10/locations" 127.0.0.1 @ 2014-06-02 13:11:32 +0100 processing locationscontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"hgjxwarawys4rp3gidzbnd3mrshcnvdltb0r6ulblbo=", "location"=>{"street_address"=>"1 01 pearl street cardiff", "google_address"=>"101 pearl street, cardiff cf24 1ne, uk", "latitude"=>"51.4866055", "longitude"=>"-3.15 24131999999554"}, "commit"=>"enter location", "skill_id"=>"10"} user load (0.8ms) select "users".* "users" "users"."id" = 6 limit 1 location load (0.6ms) select "locations".* "locations" "locations"."mappable_id" = 6 , "locations"."mappable_type" = 'user' limit 1 avatar load (0.5ms) select "avatars".* "avatars" "avatars"."avatarable_id" = 6 , "avatars"."avatarable_type" = 'use r' limit 1 profile load (0.5ms) select "profiles".* "profiles" "profiles"."user_id" = 6 limit 1 skill load (0.4ms) select "skills".* "skills" "skills"."id" = $1 limit 1 [["id", "10"]] cache (0.0ms) select "skills".* "skills" "skills"."id" = $1 limit 1 [["id", "10"]] location load (0.6ms) select "locations".* "locations" "locations"."mappable_id" = 10 , "locations"."mappable_type" = 'skill' limit 1 (0.1ms) begin sql (0.8ms) insert "locations" ("actor_id", "address", "city", "country", "country_code", "created_at", "google_address", " latitude", "locality", "longitude", "mappable_id", "mappable_type", "political", "postal_code", "postcode", "street_address", "subl ocality", "updated_at", "ward_id") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19) re turning "id" [["actor_id", nil], ["address", nil], ["city", "cardiff"], ["country", "united kingdom"], ["country_code", nil], ["cr eated_at", mon, 02 jun 2014 12:11:33 utc +00:00], ["google_address", "101 pearl street, cardiff cf24 1ne, uk"], ["latitude", 51.486 6055], ["locality", nil], ["longitude", -3.1524132], ["mappable_id", 10], ["mappable_type", "skill"], ["political", nil], ["postal_ code", "cf24 1ne"], ["postcode", nil], ["street_address", "101 pearl street"], ["sublocality", nil], ["updated_at", mon, 02 jun 201 4 12:11:33 utc +00:00], ["ward_id", nil]] ward load (0.5ms) select "wards".* "wards" "wards"."name" = 'cardiff' limit 1 (0.8ms) commit
don't know why, problem location_controller new method changed @location = @mappable.build_location @location = location.new , started work.
Comments
Post a Comment