表单提交按钮未命中电影#在 Rails 中创建



>我正在尝试在 Rails 中提交表单,但它没有创建到数据库中。我尝试在控制器操作中放置一个binding.pry,但我没有达到它。你能看看我下面的表格和我的控制器操作,让我知道我是否做错了什么吗?

<form>
<%= simple_form_for @movie do |f| %>
<div class="form-group">
<%= f.input :title, placeholder: "Movie Title", input_html: { class: 'form-control' } %>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<%= f.input :year, as: :date, 
start_year: Date.today.year,
end_year: Date.today.year - 100, 
discard_day: true, order: [:year], 
input_html: { class: 'form-control' } %>
</div>
<div class="form-group col-md-6">
<%= f.input :genre, placeholder: "Genre", input_html: { class: 'form-control' } %>
</div>
</div>
<div class="form-group">
<%= f.input :poster, placeholder: "Poster URL", input_html: { class: 'form-control' } %>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<%= f.input :director, placeholder: "Director",
input_html: { class: 'form-control' } %>
</div>
<div class="form-group col-md-6">
<%= f.input :rating, collection: 1..5, prompt: "1(bad) - 5(great)", input_html: { class: 'form-control' } %>
</div>
</div>
<div class="form-group">
<%= f.association :lists, as: :radio_buttons, input_html: { class: 'form-control' } %>
</div>
<div class="form-group">
<%= f.input :plot, as: :text, placeholder: "Plot Summary", input_html: { class: 'form-control' } %>
</div>
<div class="form-group text-center">
<%= f.button :submit, "Add Movie", class: "btn btn-primary col-md-4" 
%> 
</div>
<% end %>
</form>

我的控制器操作:

def new
@movie = Movie.new
end
def create
binding.pry
@movie = Movie.new(movie_params)
if @movie.save
redirect_to movie_path(@movie)
else
flash[:danger] = "Please try again!"
redirect_to new_movie_path
end
end
def movie_params
params.require(:movie).permit(:title, :year, :genre, :poster, :director, :plot, :list_ids)
end

这里有什么想法吗?表格将不会提交。

您需要将rating和可能lists添加到您的movie_params

您还可以清理create方法并使其更简单:

def create
@movie = Movie.new(movie_params)
if @movie.save
redirect_to @movie
else
flash[:danger] = "Please try again!"
render 'new'
end
end

使用simple_form 时不需要使用<form>

最新更新