Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9074737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:42:26+00:00 2026-06-16T18:42:26+00:00

I am trying to create a nested form rails in which will create two

  • 0

I am trying to create a nested form rails in which will create two model at the same time. The model are has follow:

Model/Events

#Data
  attr_accessible :customer_id, :description, :locations_attributes
#Relationship
  has_many :locations
  accepts_nested_attributes_for :locations, :allow_destroy => true

Model/Locations

#Data
  attr_accessible :address, :customer_id, :event_id, :latitude, :longitude
#Relationship
  belongs_to :customer
  belongs_to :event

The view his like this

<%= form_for(@event) do |f| %>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <%= f.fields_for :locations do |e| %>
    <%= e.hidden_field :longitude %>
    <%= e.hidden_field :latitude %>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

The controller looks like this

  def new
    @event = Event.new
    @location = Location.new
    ...
  def create
    @event = current_customer.events.build(params[:event])
    @event.locations.build(params[:locations])
    respond_to do |format|
    ...

The issues is that when I create the events, the form is generated, it create the events and create a locations and associated to the events, but my longitude and latitude field are empty. I must note they are float type, but i don’t understand why it shouldn’t work

Update: I notice for me to be able to access the field_for i must change the field_for to single

 <%= f.fields_for :location do |e| %>

But then when doing this i get the following error when trying to create the event

Can't mass-assign protected attributes: location

and the params is has follow

 "location"=>{"longitude"=>"-80.9460917",
 "latitude"=>"46.4350391"}

This is my javascript

function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
}
    function showPosition(position)
    {
        document.getElementById('event_location_latitude').value=position.coords.latitude;
        document.getElementById('event_location_longitude').value=position.coords.longitude;
    }

I admits its ugly but it does the jobs from application.js I called it with a function in the form

<script type="text/javascript">
   getLocation();
  </script>

Which is just before <% end %> of the form

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:42:27+00:00Added an answer on June 16, 2026 at 6:42 pm

    check this out.

    http://railscasts.com/episodes/196-nested-model-form-revised

    if you have questions after viewing it. let me know

    In your hidden field code you need to set a value. For example.

    <%= f.hidden_field :song_id, value: params[:id] %>
    <%= f.hidden_field :paid_price,  value: @song.price %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create form that contains another model in rails. I have
I am trying to make a nested model form in which I can add/delete
I'm trying to add a fields_for attribute to a nested rails form. Any time
Trying to use the nested model form in the new rails release candidate. I
I am trying to create nested routes for a project that has many boards.
I'm using Rails 2.3.2, and trying to get a nested object form to work
I'm trying to create a form handling 4 models at once with 2 nested
I am trying to create a form with Sencha Touch that will create a
I'm trying to create a nested form but I'm missing something as keep getting
I'm trying to create a form which allows me to submit new records for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.