嵌入式文档的嵌套表单

 hanliu46460 发布于 2023-01-19 08:19

我有以下型号

城市(id,name,geo {lng,lat})

地理位置(LNG,LAT)

城市模型

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

地理位置模型

class GeoLocation
  include Mongoid::Document
  include Mongoid::Timestamps

  field :lng, type: String
  field :lat, type: String

  embedded_in :city
end

城市控制员

class CitiesController < ApplicationController
  before_action :set_city, only: [:show, :edit, :update, :destroy]

  # GET /cities
  def index
    @cities = City.all
  end

  # GET /cities/1
  def show
  end

  # GET /cities/new
  def new
    @city = City.new
    @regions = Region.all.asc(:name)
    @countries = Country.all.asc(:name)
  end

  # GET /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 was successfully created.'
    else
      render action: 'new'
    end
  end

  # PATCH/PUT /cities/1
  def update
    if @city.update(city_params)
      redirect_to @city, notice: 'City was successfully updated.'
    else
      render action: 'edit'
    end
  end

  # DELETE /cities/1
  def destroy
    @city.destroy
    redirect_to cities_url, notice: 'City was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_city
      @city = City.find(params[:id])
    end

    # Only allow a 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_for(@city) do |f| %>
  <% if @city.errors.any? %>
    

<%= pluralize(@city.errors.count, "error") %> prohibited this city from being saved:

    <% @city.errors.full_messages.each do |msg| %>
  • <%= msg %>
  • <% end %>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :country %>
<%= f.collection_select :country_id, @countries, :id, :name, :prompt => "Please Select" %>
<%= f.label :region %>
<%= f.collection_select :region_id, @regions, :id, :name, :prompt => "Please Select" %>
<%= f.label :timezone %>
<%= f.text_field :timezone %>
<%= f.label :slug %>
<%= f.text_field :slug %>
<%= f.fields_for :geo_locations do |geo_location| %>
<%= geo_location.label :lag %>
<%= geo_location.text_field :lag %>
<%= geo_location.label :lat %>
<%= geo_location.text_field :lat %>
<% end %>
<%= f.submit %>
<% end %>

新观点

New city

<%= render 'form' %> <%= link_to 'Back', cities_path %>

我得到的错误

未允许的参数:geo_location

Ranjithkumar.. 5

在控制器上,用这个替换你的city_params方法,

def city_params
  params.require(:city).permit(:name, :timezone, :region_id, :country_id, :slug, :geo_location_attributes => [:id, :lag, :lat])
end

在视图上,将此" f.fields_for:geo_locations "替换为" f.fields_for:geo_location "

geo_locations_attributes中的问题.它应该是geo_location_attributes,因为这是一对一的关系.

1 个回答
  • 在控制器上,用这个替换你的city_params方法,

    def city_params
      params.require(:city).permit(:name, :timezone, :region_id, :country_id, :slug, :geo_location_attributes => [:id, :lag, :lat])
    end
    

    在视图上,将此" f.fields_for:geo_locations "替换为" f.fields_for:geo_location "

    geo_locations_attributes中的问题.它应该是geo_location_attributes,因为这是一对一的关系.

    2023-01-19 08:26 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有