0

I studied the rails tutorial by Michael Hartl and I'd like to add new service to this app.

Although I created new model, controller and views, the following error appeared when I submit f.submit "Create my schedule" in _schedule_form.html.erb.

This error may be caused by strong parameter, I guess.

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

development.log

ActionController::ParameterMissing (param is missing or the value is empty: schedule):
  app/controllers/schedules_controller.rb:30:in `schedule_params'
  app/controllers/schedules_controller.rb:9:in `create'

schedule_controller.rb

class SchedulesController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]

  def new
    @schedule = Schedule.new
  end

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

...

  private

    def schedule_params
      params.require(:schedule).permit(:title)
    end

end

views\schedules\new.html.erb

<div class="row">
  <div class="col-md-12">
    <p>Create schedule (<%= current_user.name %>)</p>
    <%= render "schedule_form" %>
  </div>
</div>

views\schedules\ _schedule_form.html.erb

<%= form_for(@schedule) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="input-group">
    <span class="input-group-addon">Title</span>
    <input type="text" class="form-control">
  </div>
  <br>
  <%= f.submit "Create my schedule", class: "btn btn-primary" %>
  <br>
<% end %>

3 Answers 3

1

The problem is that you're manually rendering the form input fields. The input field has to have a specific name for the params to get generated properly. In your case, you'd need something like:

<%= f.text_field :title %>

Have a look at the form helpers documentation for more details.

Sign up to request clarification or add additional context in comments.

Comments

1

You're not building the form with the Rails helper method and so it isn't naming your input properly. Use the text field helper:

<%= f.text_field :title %>

Comments

0

You might be missing "schedule" in your params or it is empty. As I can see you are using direct html

     <input type="text" class="form-control">

instead use rails way of using form builder object for e.g

    f.input :title, class: 'form-control'

or if you still want to use direct html use this instead

   <input type="text" class="form-control" name="schedule[title]">

Hope this helps

Comments

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.