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.
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'
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
<div class="row">
<div class="col-md-12">
<p>Create schedule (<%= current_user.name %>)</p>
<%= render "schedule_form" %>
</div>
</div>
<%= 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 %>
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.
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 %>
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 eg
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.