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

时间:2018-10-03 19:43:47

标签: ruby-on-rails ruby simple-form binding.pry

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

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

1 个答案:

答案 0 :(得分: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

使用simple_form时无需使用<form>