Search code examples
ruby-on-railsrubysimple-formbinding.pry

Form submit button not hitting movies#create in Rails


I'm attempting to submit a form in Rails, and it's not creating into the db. I tried placing a binding.pry in the controller action but I'm not reaching it. Can you take a look at my form below and my controller action and let me know if I'm doing anything wrong?

<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>

My controller actions:

  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

Any ideas here? The form will not submit.


Solution

  • You need to add rating and possibly lists into your movie_params

    You can also clean your create method and make it simpler:

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

    You shouldn't need to use <form> when using simple_form.