ruby on rails - Nested Forms for Embedded Documents -
i have following models
cities(id, name, geo {lng,lat})
geo(lng,lat)
cities model
class city include mongoid::document include mongoid::timestamps field :name, type: string field :timezone, type: string field :slug, type: string belongs_to :region belongs_to :country embeds_one :geo_location accepts_nested_attributes_for :geo_location end
geo locations model
class geolocation include mongoid::document include mongoid::timestamps field :lng, type: string field :lat, type: string embedded_in :city end
cities controller
class citiescontroller < applicationcontroller before_action :set_city, only: [:show, :edit, :update, :destroy] # /cities def index @cities = city.all end # /cities/1 def show end # /cities/new def new @city = city.new @regions = region.all.asc(:name) @countries = country.all.asc(:name) end # /cities/1/edit def edit @regions = region.all.asc(:name) @countries = country.all.asc(:name) end # post /cities def create @city = city.new(city_params) if @city.save redirect_to @city, notice: 'city created.' else render action: 'new' end end # patch/put /cities/1 def update if @city.update(city_params) redirect_to @city, notice: 'city updated.' else render action: 'edit' end end # delete /cities/1 def destroy @city.destroy redirect_to cities_url, notice: 'city destroyed.' end private # use callbacks share common setup or constraints between actions. def set_city @city = city.find(params[:id]) end # allow trusted parameter "white list" through. def city_params params.require(:city).permit(:name, :timezone, :region_id, :country_id, :slug, :geo_locations_attributes => [:id, :lag, :lat]) end end
form:
<%= form_for(@city) |f| %> <% if @city.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@city.errors.count, "error") %> prohibited city being saved:</h2> <ul> <% @city.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :country %><br> <%= f.collection_select :country_id, @countries, :id, :name, :prompt => "please select" %> </div> <div class="field"> <%= f.label :region %><br> <%= f.collection_select :region_id, @regions, :id, :name, :prompt => "please select" %> </div> <div class="field"> <%= f.label :timezone %><br> <%= f.text_field :timezone %> </div> <div class="field"> <%= f.label :slug %><br> <%= f.text_field :slug %> </div> <%= f.fields_for :geo_locations |geo_location| %> <div class="field"> <%= geo_location.label :lag %><br> <%= geo_location.text_field :lag %> </div> <div class="field"> <%= geo_location.label :lat %><br> <%= geo_location.text_field :lat %> </div> <% end %> <div class="actions"> <%= f.submit %> </div> <% end %>
new view
<h1>new city</h1> <%= render 'form' %> <%= link_to 'back', cities_path %>
the error getting
unpermitted parameters: geo_location
on controller, replace city_params method this,
def city_params params.require(:city).permit(:name, :timezone, :region_id, :country_id, :slug, :geo_location_attributes => [:id, :lag, :lat]) end
on view, replace "f.fields_for :geo_locations" "f.fields_for :geo_location"
problem in geo_locations_attributes. should geo_location_attributes one-to-one relationship.
Comments
Post a Comment