0

我正在尝试在 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

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

4

1 回答 1

0

您需要添加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

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

于 2018-10-03T20:04:51.593 回答