ruby-on-rails - 嵌入文档的嵌套表单

标签 ruby-on-rails ruby-on-rails-4 mongoid

我有以下型号

cities(id, name, geo {lng,lat})

geo(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

城市 Controller

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? %>
    <div id="error_explanation">
      <h2><%= pluralize(@city.errors.count, "error") %> prohibited this city from being saved:</h2>

      <ul>
      <% @city.errors.full_messages.each do |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 do |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 %>

新 View

<h1>New city</h1>

<%= render 'form' %>

<%= link_to 'Back', cities_path %>

我遇到的错误

Unpermitted parameters: geo_location

最佳答案

在 Controller 上,用此替换您的 city_params 方法,

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

在 View 中,将此“f.fields_for :geo_locations”替换为“f.fields_for :geo_location

geo_locations_attributes 出现问题。它应该是geo_location_attributes,因为这是一对一关系

关于ruby-on-rails - 嵌入文档的嵌套表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22294906/

相关文章:

ruby-on-rails - Mongoid 嵌入式文档中的时间戳

ruby-on-rails - 创建 Rails 新项目时出错 (../config/boot (LoadError)

ruby-on-rails - RSpec/FactoryGirl - Rails STI - 平等

ruby-on-rails - Mysql2::错误:未知列 Rails

ruby-on-rails - 我可以重命名 routes.rb 中的资源吗?

ruby-on-rails - 如何强制轻便摩托车从延迟运行的 Mongo 辅助副本集成员中读取

ruby-on-rails - 在文本 block 中查找和替换

ruby-on-rails - Heroku LoadError : libruby. so.2.2

ruby-on-rails - Ruby on Rails 记住我登录不工作

ruby-on-rails - Ruby on Rails 和 NoSQL,添加字段