ROR Active Record不能正确保存数据

时间:2017-11-03 15:17:12

标签: ruby-on-rails database activerecord simple-form-for

大家好我有问题而且我没有'知道如何解决它,我在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)我已经添加了对行程表的municipality_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

1 个答案:

答案 0 :(得分:0)

尝试在控制器中更新这个

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

您需要始终在末尾允许参数中的JSON值