0

I've got an object/param that I'm saving into my database. But while I do that I'd like to get the first row in that object to do some Javascript things with. So what I'm receiving in my view from the Javascript object(Which is being send by an AJAX request) if i do this:

<% @markers.each do |marker| %>
  <%= marker%>
<% end %>

Is this record:

["triplocations_attributes", {"0"=>{"latlng"=>"53.20192541983673, 5.535024029052693", "title"=>"first", "description"=>"first"}, "1"=>{"latlng"=>"53.18896756239149, 5.536568981445271", "title"=>"2nd", "description"=>"2nd"}}]

Now, what I would like is to get the latlng from the first(0) row. Can anyone tell me how?

What I've tried so far didn't work:

<%= marker.triplocations_attributes %>
<%= marker[0].title %>
<%= marker.title %>
CaptainCarl
  • 3,411
  • 6
  • 38
  • 71
  • That 'record' really doesn't look like it's in the right format for params. Params are normally a hash. – sevenseacat Oct 30 '12 at 16:01
  • It's what gives me the opportunity to get it in my database, which works perfectly fine. So I'm assuming that the format is fine thusfar. Aslong as you don't need to go in it yourself :-). The way i save it to my DB is as following by the way: trip = Trip.create(params[:markers]) – CaptainCarl Oct 30 '12 at 16:05

1 Answers1

1

It seems reasonable to suppose that you have something like

class Marker < ActiveRecord::Base
  has_many :triplocations
  ...
end

In that case you need to access

marker.triplocations[0].title
marker.triplocations[0].description
...
rewritten
  • 16,280
  • 2
  • 47
  • 50
  • Unfortunately: `undefined method `triplocations' for #` It is because i don't get this data from my database but from a JS object sent through AJAX – CaptainCarl Oct 30 '12 at 16:09
  • 1
    You said you are saving to the database in the original post, now you say that you don't get the data from the database... please let people help you by being clear. You don't say what `@markers` is, nor what are your models, nor how you save the data into it, if you provide this info you will get some more help. – rewritten Oct 30 '12 at 16:13
  • As stated I am saving to the database. But BEFORE I do that I'd like to get the first row. As a Rails newbie and a PHP-rookie I'm assuming that there's no need for a whole lot of info to get an answer about how to get a first row to be honest. I mean, in PHP it's just $row[0]. So how much harder can it be? Anywho; I've shuffled some things around and saving the triplocations beforehand and used your method after which worked perfectly fine. – CaptainCarl Oct 30 '12 at 16:18
  • The reason of this is that PHP is not a real language... ehem, sorry, I didn't mean it... The point is that rails accepts a "serialized" version of dependent objects, in your case `triplocations_attributes`, from which it is able to build actual object (in an object-oriented languange sense) and save them to the database. The fact that you are using ERB (`<%= ... %>`) means that you are in a view, so all the controller work is supposed to be done, so the objects should have been already saved. If you are in the view, and you haven't saved your object yet, then you are getting something wrong. – rewritten Oct 30 '12 at 17:08
  • And, by the way, until you save something to the database, there is no such thing as a "row".... – rewritten Oct 30 '12 at 17:10