1

嗨,伙计们,我有一个问题,我不知道如何解决它,我对 ROR 的世界真的很陌生。

序言:每个市镇都有很多行程,所以:

1)我用这个迁移创建了行程表:

class CreateItineraries < ActiveRecord::Migration
  def change
    create_table :itineraries do |t|
      t.string :title
      t.string :json
      t.integer :muncipality_id
      t.text :description
      t.boolean :published, :default => true, :null => false

      t.timestamps null: false
    end
  end
end

2)我已经添加了对路线表的 cityity_id 引用,这个迁移:

class AddMunicipalityIdToItineraries < ActiveRecord::Migration
  def change
    add_reference :itineraries, :municipality, index: true, foreign_key: true
  end
end

3)为行程翻译创建了另一个表格

class AddTranslationTablesForItineraries < ActiveRecord::Migration
  def up
    Itinerary.create_translation_table!({
                                                  :title => :string
                                              }, {
                                                  :migrate_data => true
                                              })
  end

  def down
    add_column :itineraries, :title, :string
    Itinerary.drop_translation_table! :migrate_data => true
  end
end

现在,问题是当我尝试保存相对 simple_form 中的数据时,它只将行程的标题保存在翻译表中,为什么?

这里是 simple_form 的代码:

<%= simple_form_for [:admin, @municipality, @itinerary], url: @url, :html => {:class => ''} do |f| %>
    <%= render 'application/translation_form_heading' %>
    # ...
    <%= f.input :title, required: true %>
    <%= f.input :description, label: 'description', :as => :ckeditor, :input_html => {ckeditor: {toolbar: get_toolbar('default')},:rows => 15} %>
    <%= f.input :json, required: true, label: 'GeoJSON', as: :text, :input_html => {:rows => 15} %>

    # ...
    <button type="submit" class="btn btn-primary pull-right">Save itinerary</button>    
<% end %>

也许这是一个新手问题,但我真的不知道如何解决它,谢谢!

编辑:这里是itinerariesController的代码:

class Admin::ItinerariesController < InheritedResources::Base
  layout 'admin'
  before_filter :authenticate_admin!
  before_filter :set_page_title
  load_and_authorize_resource
  actions :all, :except => [:show]

  belongs_to :municipality

  def index
    @itineraries = Itinerary.ordered
  end

  def new
    @url = admin_municipality_itineraries_path(params[:municipality_id])
    new!
  end

  def edit
    @url = admin_municipality_itinerary_path(params[:municipality_id], params[:id])
    edit!
  end

  def update
    unless params[:translate_to].blank?
      I18n.with_locale(params[:translate_to]) {
        update!
      }
    else
      update!
    end
  end

  def set_page_title
    @page_title = PointOfInterest.model_name.human(:count => 2).titleize
  end

  def create
    create! {
      admin_municipality_itineraries_path
    }
  end

  private
  def permitted_params
    params.permit(:itinerary => [:id, :title, :json,:description, :municipality_id] )
  end

end
4

1 回答 1

0

尝试在控制器中更新这个

 private 
 def permitted_params  
   params.permit(:itinerary => [:id, :title, :description, :municipality_id, :json] ) 
 end

您需要始终在最后允许参数中的 JSON 值

于 2017-11-04T02:35:46.203 回答