0

Unpermitted parameter is displayed when I try to create new data. Althogh there are many similar questions, I can't find out how to solve.

log

Processing by SchedulesController#create as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test", "departure_date"=>"2016-06-24", "room"=>{"room"=>"test name"}}, "commit"=>"Create my schedule"}
  Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test", "departure_date"=>"2016-06-24", "room"=>{"room"=>"test name"}}, "commit"=>"Create my schedule"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
Unpermitted parameter: room

models

schedule.rb

class Schedule < ActiveRecord::Base
  belongs_to :user
  has_many :rooms, inverse_of: :schedule, dependent: :destroy
  has_many :amounts, inverse_of: :schedule, dependent: :destroy
  accepts_nested_attributes_for :rooms, allow_destroy: true

rooms.rb

class Room < ActiveRecord::Base
  belongs_to :schedule, inverse_of: :rooms
  has_many :events, inverse_of: :room, dependent: :destroy
  has_many :amounts, inverse_of: :room, dependent: :destroy
  accepts_nested_attributes_for :events, allow_destroy: true

controller

schedule_controller.rb

  def new
    @schedule = Schedule.new
    room = @schedule.rooms.build
  end

  def create
    @schedule = current_user.schedules.build(schedule_params)
    if @schedule.save
      flash[:success] = "schedule created!"
      redirect_to schedule_path(@schedule)
    else
      render 'new'
    end
  end

...

  def schedule_params
    params.require(:schedule).permit(
      :title, :departure_date, 
      rooms_attributes: [
        :id, :_destroy, :room, :room_address, :schedule_id, :day,  
        events_attributes: [
          :id, :_destroy, :start_at, :end_at, :title, :detail, :category,                      
          amounts_attributes: [
            :room_id, :event_id, :id, :_destroy, :ccy, :amount
          ]
        ]
      ]
    )
  end

view

/schedules/new.html.erb

...
<%= form_for(@schedule) do |f| %>
  <%= render 'schedule_form', f: f %>
  <%= f.submit "Create my schedule", class: "btn btn-primary" %>
...

/schedules/ _schedule_form.html.erb

...
  <%= f.label :title %>
  <%= f.text_field :title, class: 'form-control' %>
...

<%= f.fields_for(:room) do |a| %>
  <%= a.text_field :room %>        #room column exist in the rooms table
<% end %>

It would be appreciated if you could give me any suggestion.

1 Answer 1

1

You are not getting the params in proper format

Parameters: {"utf8"=>"?", "authenticity_token"=>"xxx", "schedule"=>{"title"=>"test", "departure_date"=>"2016-06-24", "room"=>{"room"=>"test name"}}, "commit"=>"Create my schedule"}
Unpermitted parameter: room

your params should contain rooms_attributes instead room

Change f.fields_for(:room) to f.fields_for(:rooms) in /schedules/ _schedule_form.html.erb

...
  <%= f.label :title %>
  <%= f.text_field :title, class: 'form-control' %>
...

<%= f.fields_for(:rooms) do |a| %>
  <%= a.text_field :room %>        #room column exist in the rooms table
<% end %>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your quick answer, @Deepak. I'm sorry I forgot to input my strong parameter. I add it in controller. Could you check again?
@SamuraiBlue Then the problem is in your form because you should receive oarams as rooms_attributes which you are not. Try changing f.fields_for(:rooms)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.