ruby on rails - how to permit an array with strong parameters -
i have functioning rails 3 app uses has_many :through associations not, remake rails 4 app, letting me save ids associated model in rails 4 version.
these 3 relevant models same 2 versions.
categorization.rb
class categorization < activerecord::base belongs_to :question belongs_to :category end
question.rb
has_many :categorizations has_many :categories, through: :categorizations
category.rb
has_many :categorizations has_many :questions, through: :categorizations
in both apps, category ids getting passed create action this
"question"=>{"question_content"=>"how spell car?", "question_details"=>"blah ", "category_ids"=>["", "2"],
in rails 3 app, when create new question, inserts questions table , categorizations table
sql (82.1ms) insert "questions" ("accepted_answer_id", "city", "created_at", "details", "province", "province_id", "question", "updated_at", "user_id") values (?, ?, ?, ?, ?, ?, ?, ?, ?) [["accepted_answer_id", nil], ["city", "dd"], ["created_at", tue, 14 may 2013 17:10:25 utc +00:00], ["details", "greyound?"], ["province", nil], ["province_id", 2], ["question", "whos' biggest dog in world"], ["updated_at", tue, 14 may 2013 17:10:25 utc +00:00], ["user_id", 53]] sql (0.4ms) insert "categorizations" ("category_id", "created_at", "question_id", "updated_at") values (?, ?, ?, ?) [["category_id", 2], ["created_at", tue, 14 may 2013 17:10:25 utc +00:00], ["question_id", 66], ["updated_at", tue, 14 may 2013 17:10:25 utc +00:00]]
in rails 4 app, after processes parameters in questioncontroller#create, i'm getting error in server logs
unpermitted parameters: category_ids
and question getting inserted questions table
(0.2ms) begin sql (67.6ms) insert "questions" ("city", "created_at", "province_id", "question_content", "question_details", "updated_at", "user_id") values ($1, $2, $3, $4, $5, $6, $7) returning "id" [["city", "dd"], ["created_at", tue, 14 may 2013 17:17:53 utc +00:00], ["province_id", 3], ["question_content", "how's car?"], ["question_details", "is runnign"], ["updated_at", tue, 14 may 2013 17:17:53 utc +00:00], ["user_id", 12]] (31.9ms) commit
although not storing category_ids on questions model, set category_ids permitted parameter in questions_controller
def question_params params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids) end
can explain how i'm supposed save category_ids? note, there no create action in categories_controller.rb of either app.
these 3 tables same in both apps
create_table "questions", force: true |t| t.text "question_details" t.string "question_content" t.integer "user_id" t.integer "accepted_answer_id" t.datetime "created_at" t.datetime "updated_at" t.integer "province_id" t.string "city" end create_table "categories", force: true |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "categorizations", force: true |t| t.integer "category_id" t.integer "question_id" t.datetime "created_at" t.datetime "updated_at" end
update
this create action rails 3 app
def create @question = question.new(params[:question]) respond_to |format| if @question.save format.html { redirect_to @question, notice: 'question created.' } format.json { render json: @question, status: :created, location: @question } else format.html { render action: "new" } format.json { render json: @question.errors, status: :unprocessable_entity } end end end
this create action rails 4 app
def create @question = question.new(question_params) respond_to |format| if @question.save format.html { redirect_to @question, notice: 'question created.' } format.json { render json: @question, status: :created, location: @question } else format.html { render action: "new" } format.json { render json: @question.errors, status: :unprocessable_entity } end end end
this question_params method
private def question_params params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids) end
this https://github.com/rails/strong_parameters seems relevant section of docs:
the permitted scalar types string, symbol, nilclass, numeric, trueclass, falseclass, date, time, datetime, stringio, io, actiondispatch::http::uploadedfile , rack::test::uploadedfile.
to declare value in params must array of permitted scalar values map key empty array:
params.permit(:id => [])
in app, category_ids passed create action in array
"category_ids"=>["", "2"],
therefore, when declaring strong parameters, explicitly set category_ids array
params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids => [])
works now!
Comments
Post a Comment