ruby-on-railsrubyrails-geocoder

How can a user update another model's table's values using a form?


I have a user that contains their addresses. Now, I have another table, called search address, that contains a search_address.

This should be a one to one relationship, where a user can only create one search address on the search address table.

I want it so a user can submit a form, and create, in the address search table, a search_address. Afterwards, I want to callback that search_address's latitude and longitude (I'm using the geocoder gem).

This is the code I have so far:

Schema:

  create_table "users", force: :cascade do |t|
    t.string   "address"
    t.float    "latitude"
    t.float    "longitude"
  end
  create_table "search_addresses", force: :cascade do |t|
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.string   "search_address"
    t.float    "search_latitude"
    t.float    "search_longitude"
  end

User model:

class User < ActiveRecord::Base

geocoded_by :address
after_validation :geocode

end

Search Address model:

class SearchAddress < ActiveRecord::Base
  belongs_to :user

  geocoded_by :search_address, :latitude => :search_latitude, :longitude => :search_longitude

end

My current view (I based this answer off another Stack answer, but it isn't working):

<%= semantic_form_for @users do |f| %>

<%= fields_for @user.searchaddresses do |g| %>

    <div class="field">
      <%= g.label :search_address, 'Search Address' %>
      <br/>
      <%= g.text_field :search_address, :class => "form-control" %>
    </div>

  <%= f.submit 'Save Search Address' %>
  <% end %>
  <% end %>

Also, my intended view would display something like this:

<%= @user.searchaddress.latitude %>
<%= @user.searchaddress.longitude %>

Based on this information, what should I do to the form so a user can add in one search address, and then have another view show the latitude and longitude of this search address?


Solution

  • 1 => Use Nested Attributes for User

    class User < ActiveRecord::Base
      has_many : search_addresses, dependent: :destroy
      geocoded_by :address
      after_validation :geocode
      accepts_nested_attributes_for :search_addresses
    end
    

    2 => Fix Typo

    <%= semantic_form_for @user do |f| %>
    
      <%= fields_for @user.search_addresses do |g| %>
        <div class="field">
          <%= g.label :search_address, 'Search Address' %>
          <br/>
          <%= g.text_field :search_address, :class => "form-control" %>
        </div>
      <% end %>
    
      <%= f.submit 'Save Search Address' %>
    <% end %>
    

    3 => users_controller.rb

    def update
      #fetch user to be updated along with search_addresses
      @user.update(update_user_params)
      #after update
    end
    
    private
      def update_user_params
        params.require(:user).permit(search_addresses_attributes: [:search_address])
      end
    

    References: - Ruby on Rails Nested Attributes,

    Active Record Nested Attributes