Reputation: 6045
I'm currently using an API and not a database, and I want to be as close as ActiveRecord, so I decided to go ahead and do exactly like this railscast here: http://railscasts.com/episodes/219-active-model
So far, my save method works well, so I can save data to the API. My problem is with the edit, my find method seems to be the problem... Here is some code!
Edit method in my controller
def edit
@parking = Parking.find(params[:id])
end
Whole model
class Parking
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :address, :city, :longitude, :latitude, :contributor_name, :contributor_email
validates_presence_of :name, :address, :city, :longitude, :latitude
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def self.find(id)
parking = Parse.get("Parking", id.to_s) // API Call
name = parking["name"]
address = parking["address"]
city = parking["city"]
longitude = parking["location"]["longitude"]
latitude = parking["location"]["latitude"]
contributor_name = parking["contributorName"]
contributor_email = parking["contributorEmail"]
return self
end
def save
if (!valid?)
return false
else
parking = Parse::Object.new("Parking")
data =
{
:longitude => 40.0,
:latitude => -30.0
}
point = Parse::GeoPoint.new(data)
parking["location"] = point
parking["name"] = name
parking["address"] = address
parking["city"] = city
parking["contributorName"] = contributor_name
parking["contributorEmail"] = contributor_email
if (parking.save)
return true
end
end
end
def persisted?
false
end
end
Here is the error I currently get:
undefined method `to_key' for Parking:Class
Extracted source (around line #1):
1: <%= form_for(@parking, :html => { :class => "form-horizontal"}) do |f| %>
2: <% if @parking.errors.any? %>
3: <div class="alert alert-error fade in">
4: <a class="close" data-dismiss="alert">×</a>**
If anybody as suggestions, I'm open to any ideas really, I'm beginning with rails :)
Thanks!
EDIT:
When I do in my controller edit method something like:
def edit
@parking = Parking.new
@parking.name = "foo"
@parking.address = "foo"
@parking.city = "foo"
@parking.longitude = "foo"
@parking.latitude = "foo"
end
My view load foo in every fields no problemo, so the problem is I must be doing something wrong with the find method :)
Upvotes: 3
Views: 4668
Reputation: 11705
One problem is that your find
method is a class method (by virtue of being 'self.find'), meaning it does not operate on an instance of the class and therefore has no knowledge of instance variables/methods such as name
, address
etc.
A better way to implement find
is to instantiate a new instance of Parking
, and populate it's variables, then return it e.g.
def self.find(id)
raw = Parse.get("Parking", id.to_s)
parking = Parking.new
parking.name = raw["name"]
# etc for the others
parking # Return the newly-created instance
end
This doesn't explain the 'undefined method' you're currently seeing, you may need to post up more detail to get an answer for that, particularly a full backtrace from the exception to see which bit of code is actually raising it. From the information you've supplied I'd guess that something within the Parse.get
method is causing it.
Upvotes: 4