表单提交按钮未点击电影#create in Rails

Form submit button not hitting movies#create in 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>