I have such problem. I'm using google_maps_for_rails gem and it is can't find location correctly. I run all instructions from documentation.
Here is my application.erb:
<%= yield :scripts%>
My model:
attr_accessible :address, :latitude, :longtitude
acts_as_gmappable
def gmaps4rails_address
self.address
end
but when I create location I get object with nil longtitude. Here is log:
INSERT INTO "locations" ("address", "created_at", "latitude", "longtitude", "name",
"updated_at") VALUES (?, ?, ?, ?, ?, ?) [["address", "Lviv"], ["created_at", Thu, 07
Feb 2013 10:56:45 UTC +00:00], ["latitude", 49.839683], ["longtitude", nil], ["name",
nil], ["updated_at", Thu, 07 Feb 2013 10:56:45 UTC +00:00]]
Can someone help me ?
See your code:
attr_accessible :address, :latitude, :longtitude
Typo: longtitude
Note that you can personalize the fields according to my doc:
acts_as_gmappable :lat => "latitude", :lng => "longtitude"
Related
I am using Simple_form with Decent_exposure, Strong_parameters I have the following setup and I can post to households but nothing gets posted to neighbors
model
class Household < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
has_many :neighbors
accepts_nested_attributes_for :neighbors
view
= simple_nested_form_for household do |f|
= f.input :household_name
= f.simple_fields_for :neighbor, Neighbor.new do |neighbor_form|
= neighbor_form.input :first_name
= neighbor_form.input :middle_name
= neighbor_form.input :last_name
= neighbor_form.input :address
= f.button :submit
Based on the following from my log it looks like the form is working but it is not saving to neighbors the nested model - I have tried both simple_fields_for :neighbor do and simple_fields_for :neighbor, Neighbor.new do hoping that creating a new neighbor would help but it doesn't.
Parameters:{"utf8"=>"✓",
authenticity_token"=>"cVTteqPFa0JMoFi/ys0wAmNIQghubADv5lbPBr6hyq0=",
"household"=> {"household_name"=>"Deew", "neighbor"=>{"first_name"=>"Bill",
"middle_name"=>"", "last_name"=>"Ew", "street"=>"we"}}, "commit"=>"Create Household"}
(0.1ms) begin transactionSQL (0.7ms) INSERT INTO "households" ("created_at",
"household_name", "name", "updated_at") VALUES (?, ?, ?, ?)
[["created_at", Sun, 03 Feb 2013 03:02:56 UTC +00:00], ["household_name", "Deew"],
["name", nil], ["updated_at", Sun, 03 Feb 2013 03:02:56 UTC +00:00]](0.8ms)
commit transaction
Make sure that you include neighbors_attributes in the allowable parameters.
So after a merge, my RoR 3 project no longer does 'creates' correctly. Default attributes get set correctly, but not the ones I pass in:
1.9.3-p125 :020 > f=Ifilter.create(:name => "test2", :regex => "()" )
SQL (101.5ms) INSERT INTO "ifilters" ("created_at", "name", "regex", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sat, 10 Mar 2012 03:36:24 UTC +00:00], ["name", nil], ["regex", nil], ["updated_at", Sat, 10 Mar 2012 03:36:24 UTC +00:00]]
=> #<Ifilter id: 2, name: nil, regex: nil, created_at: "2012-03-10 03:36:24", updated_at: "2012-03-10 03:36:24">
However, save still works:
1.9.3-p125 :021 > f=Ifilter.new
=> #<Ifilter id: nil, name: nil, regex: nil, created_at: nil, updated_at: nil>
1.9.3-p125 :022 > f.name = "test"
=> "test"
1.9.3-p125 :023 > f.regex = "()"
=> "()"
1.9.3-p125 :024 > f.save
SQL (4.8ms) INSERT INTO "ifilters" ("created_at", "name", "regex", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sat, 10 Mar 2012 04:13:10 UTC +00:00], ["name", "test"], ["regex", "()"], ["updated_at", Sat, 10 Mar 2012 04:13:10 UTC +00:00]]
=> true
What's going on?
Thanks!
Looks like you may have attr_accessible on your model. http://api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html#method-i-attr_accessible
If you are using it, attributes that aren't explicitly listed won't get set via #new, #create, and #update_attributes (and a few more).
This usually happens when you have attribute whitelisting enabled. Make sure you define the attributes that are settable via mass-assignment in your model via the attr_accessible macro:
class Ifilter
attr_accessible :name, :regex
...
end
I'm trying to do the following in my Rails 3 model:
require 'securerandom'
class Contest < ActiveRecord::Base
attr_accessor :key
before_create :generate_key
private
def generate_key
self.key = SecureRandom.hex(3)
end
end
However when I create a Contest, all the fields in my table appear to be correct except the key which remains nil in my DB.
More Info:
In my rails server log, I see the following when I create a contest through my "create contest form"
SQL (0.5ms) INSERT INTO "contests" ("category", "created_at", "description", "key", "price", "status", "time", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["category", "camisas"], ["created_at", Wed, 15 Feb 2012 18:57:16 UTC +00:00], ["description", "test_description"], ["key", nil], ["price", 111], ["status", "In process"], ["time", "2sem"], ["title", "test_contest"], ["updated_at", Wed, 15 Feb 2012 18:57:16 UTC +00:00], ["user_id", 5]]
Note the ["key", nil]
But that should be correct right?, I would guess that the key will be added by the Contest.rb :before_create callback?
Maybe I'm miss-using SecureRandom?
Why are you using attr_accessor? This method is actually going to be defining both a setter and getter method for a key, keeping track of a virtual attribute.
Perhaps you meant to use attr_accessible, but even then that is unnecessary, because you're setting the attribute directly.
Really, you don't need either attr_accessor :key or attr_accessible :key in your model at all if you're just setting it like this. Remove the attr_accessor and it will work.
I think you should write attr_accessible <list accessible params> for example:
attr_accessible :key, :category
I initialize an AdUnit object au with
au = AdUnit.new( cp )
where cp is equal to:
{:name=>"second56", :description=>nil, :target_window=>"BLANK", :explicitly_targeted=>false, :ad_unit_sizes_attributes=>[{:height=>90, :width=>728, :is_aspect_ratio=>false, :environment_type=>"BROWSER"}], :dfp_id=>"22319511", :parent_id_dfp=>"22261791"}
and the resulting object au is
#<AdUnit id: nil, dfp_id: "22319511", parent_id_dfp: "22261791", parent_id_bulk: nil, name: "second56", description: nil, target_window: "BLANK", explicitly_targeted: false, created_at: nil, updated_at: nil>
At this point, au.valid? and au.new_record? both return true.
If I do au.save (or au.save! for that matter) the result is nil and nothing is saved to the database. but if I do
aud = au.dup
aud.save
the result is true and the record is saved.
I can save my object with the duplicate workaround, but this looks really weird to me. can anybody give any ideas as to why is this happening? Below the SQL fragments from the 2 save statements from Rails console.
Thanks in advance all Rails gurus.
Returning nil and not saving:
SQL (1.0ms) INSERT INTO "ad_units" ("created_at", "description", "dfp_id", "explicitly_targeted", "name", "parent_id_bulk", "parent_id_dfp", "target_window", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Wed, 04 Jan 2012 17:28:37 UTC +00:00], ["description", nil], ["dfp_id", "22400631"], ["explicitly_targeted", false], ["name", "zapiFirstLevel366"], ["parent_id_bulk", nil], ["parent_id_dfp", "1166751"], ["target_window", "BLANK"], ["updated_at", Wed, 04 Jan 2012 17:28:37 UTC +00:00]]
=> nil
Returning true and saving:
SQL (0.9ms) INSERT INTO "ad_units" ("created_at", "description", "dfp_id", "explicitly_targeted", "name", "parent_id_bulk", "parent_id_dfp", "target_window", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", Wed, 04 Jan 2012 17:29:58 UTC +00:00], ["description", nil], ["dfp_id", "22400631"], ["explicitly_targeted", false], ["name", "zapiFirstLevel366"], ["parent_id_bulk", nil], ["parent_id_dfp", "1166751"], ["target_window", "BLANK"], ["updated_at", Wed, 04 Jan 2012 17:29:58 UTC +00:00]]
=> true
OK, this was caused by the usage of accepts_nested_attributes_for when AdUnit has a has_and_belongs_to_many relationship with AdUnitSize. So I got to learn that these 2 do not work together nicely.
The only thing is, it might be good to have a warning somewhere, instead of having to find out about it from .save returning nil on a valid? = true object instead of true or false.
Using save! raises an exception if the record is invalid. It doesn't return anything.
If you need a return value use only save.
Basically if save! didn't raise, the record is saved successfuly. If you don't see it, then you're probably loading it wrong.
I am using the acts_as_list gem but when creating new Object the position should automatic increase in size and order position ASC.
LessonPage Model
class LessonPage < ActiveRecord::Base
acts_as_list :scope => 'lesson_id = #{lesson_id}'
default_scope order('position ASC')
end
Select
SELECT "lesson_pages".* FROM "lesson_pages" ORDER BY position ASC, "lesson_pages".id desc
Insert - first
INSERT INTO "lesson_pages" ("created_at", "lesson_id", "position", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sun, 16 Oct 2011 11:55:27 CEST +02:00], ["lesson_id", 2], ["position", 1], ["updated_at", Sun, 16 Oct 2011 11:55:27 CEST +02:00]]
Second
INSERT INTO "lesson_pages" ("created_at", "lesson_id", "position", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sun, 16 Oct 2011 11:57:15 CEST +02:00], ["lesson_id", 2], ["position", 2], ["updated_at", Sun, 16 Oct 2011 11:57:15 CEST +02:00]]
Third
INSERT INTO "lesson_pages" ("created_at", "lesson_id", "position", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sun, 16 Oct 2011 11:58:13 CEST +02:00], ["lesson_id", 2], ["position", 2], ["updated_at", Sun, 16 Oct 2011 11:58:13 CEST +02:00]]
Here it fails, because position should be 3 and not 2. How do I fix this?
thanks!!
I think it was fixed by this commit, but there is no release after this fix. I think you should use the code from github.
use this:
gem 'acts_as_list', :git => 'https://github.com/swanandp/acts_as_list.git'