2
votes

For some reason when I pass in the form data it is saving to the database as nil. I'm using rails 4 and am using strong parameters and everything looks good according to the server but in the end it winds up saving as nil. All help is greatly appreciated, I'm new to rails and this one has me stumped. I also don't currently have any validations in the model trying to get this to work.

Controller Code

def create
    @movie = Movie.new(params[movie_params])
    if @movie.save
       redirect_to @movie
    else
      flash[:error] = "Didn't save"
      render 'new'
    end 
  end

private def movie_params params.require(:movie).permit(:title, :rating) end

Form Code
<%= form_for(@movie) do |f|  %>
    <%= f.label :title %>
    <%= f.text_area :title %>
    <%= f.label :rating %>
    <%= f.text_area :rating %>
    <%= f.submit "Submit" %>
<% end %>
1

1 Answers

11
votes

Change:

@movie = Movie.new(params[movie_params])

To:

@movie = Movie.new(movie_params)

movie_params is a subset of params already, so there's no reason to pass it back in to params again - doing so will result in a missing key lookup (which returns nil as you are discovering).