rich
rich

Reputation: 2136

Rails 4 Form Data Is Saving as Nil

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

Upvotes: 2

Views: 1762

Answers (1)

PinnyM
PinnyM

Reputation: 35541

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

Upvotes: 11

Related Questions