Создатель должен существовать ошибка Ruby on Rails

У меня есть две модели, проблемы и пользователь

Пользователь:

class User < ApplicationRecord
    has_many :created_issues, :class_name => 'Issue', :foreign_key => 'creator_id'
    has_many :assigned_issues, :class_name => 'Issue', :foreign_key => 'assigned_id'
end

Проблема:

class Issue < ApplicationRecord
    belongs_to     :creator,
                   :class_name => "User",
                   :foreign_key  => "creator_id"

    belongs_to     :assigned,
                   :class_name => "User",
                   :foreign_key  => "assigned_id" 
end

Файл переноса:

class CreateIssues < ActiveRecord::Migration[5.1]
  def change
    create_table :issues do |t|
      t.string :title
      t.text :description
      t.integer :assigned_id
      t.string :tipo
      t.string :prioridad
      t.string :estado
      t.references :creator
      t.references :assigned

     add_foreign_key :issues, :users, column: :creator_id, primary_key: :id
     add_foreign_key :issues, :users, column: :assigned_id, primary_key: :id
      t.timestamps
    end
  end
end

Часть схемы:

ActiveRecord::Schema.define(version: 20171030104901) do

create_table "issues", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.integer "assigned_id"
    t.string "tipo"
    t.string "prioridad"
    t.string "estado"
    t.integer "creator_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false`enter code here`
    t.index ["assigned_id"], name: "index_issues_on_assigned_id"
    t.index ["creator_id"], name: "index_issues_on_creator_id"
  end

конец

в форме я получаю, что поле Creator должно существовать, но я прекрасно вижу его во вставке:

Параметры: {"utf8"=>"✓", "authenticity_token"=>"2yY/0x6961iIb9PxcyQAKHUSRqEj+zwQ91uPwHibU9tWjEXqiKMqp3vwzI70FVI2agtYhPgljFPDZjVZV4cmyg==", "issue"=>{"title"=>"asdsad", "description"=>"asdsa", "creator_id"=>"2", "tipo"=>"adas", "prioridad"=>"dsdaasd", "assigned_id"=>"3"}, "commit"=>"Create Issue"}
Недопустимый параметр: :creator_id
(0,1 мс) начало транзакции
Пользовательская загрузка (0,3 мс) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
(0,1 мс) откат транзакции

любая помощь ?


person Alberto    schedule 08.11.2017    source источник


Ответы (2)


Ваш Creator_id не внесен в белый список:

Unpermitted parameter: :creator_id

Вы должны опубликовать свой код контроллера там, где вы делаете params.require(...).permit(...), чтобы мы могли видеть, что там.

Это должно выглядеть примерно так:

def issue_params
  params.
    require(:issue).
    permit(:title, :description, :creator_id, :tipo, :prioridad, :assigned_id)
end
person jvillian    schedule 08.11.2017

Если вы используете драгоценный камень strong_parameters, то ответ jvillian будет работать. Если нет (что вполне возможно, так как он не использовался широко до Rails 4), вам может потребоваться добавить attr_accessible :creator_id в вашу модель задачи.

person NM Pennypacker    schedule 08.11.2017