kyle kyle - 7 months ago 47
Ruby Question

Failure/Error: click_button "Add Movie" ActionController::ParameterMissing: param is missing or the value is empty: movie

I've been looking at other answers and I'm still getting this error with params missing. I'm guessing it's a paperclip issue, but after searching through the docs, I can't find anything resolving this issue.

My Feature Spec

scenario "An Admin creates a new movie" do
visit "/"
click_link "Add New Movie"```

fill_in "Title", with: "Creating first movie"
fill_in "Synopsis", with: "Lorem Ipsum"
fill_in "Year Released", with: "Date"
click_button "Add Movie"

expect(page).to have_content("Movie has been created")
expect(page.current_path).to eq(root_path)
expect(page).to have_content("Created by: #{@kyle.email}")```

end


My movie model

class Movie < ApplicationRecord
has_many :comments
belongs_to :admin

validates :title, presence: true
validates :synopsis, presence: true
validates :year_released, presence: true

has_attached_file :photo, styles: { medium: "300x200>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"


validates_attachment_file_name :photo, matches: [/png\Z/, /jpe?g\Z/]```

My Controller

def update
if @movie.update(movie_params)
flash[:success] = "Movie has been updated"
redirect_to @movie
else
flash.now[:danger] = "Movie has not been updated"
render :edit
end
end

private

def movie_params
params.require(:movie).permit(:title, :synopsis, :year_released, :photo)
end


My _form.html.erb

<div class="form-group">
<div class='control-label col-md-3'>
<%= f.label :photo %>
</div>
<div class='col-md-10'>
<%= f.file_field :photo, class: "btn btn-primary btn-sl pull-left" %>
</div>

<div class='form-group'>
<div class='col-md-offset-1 col-md-11'>
<%= f.submit "Add Movie", class: "btn btn-primary btn-lg pull-right" %>
</div>




The error

Failure/Error: click_button "Add Movie"
ActionController::ParameterMissing:
param is missing or the value is empty: movie```


My Github if neccessary.

Answer

I assume that's because you're using require in strong_params.

I changed your code to

def movie_params
  params.fetch(:movie, {}).permit(:title, :synopsis, :year_released, :photo)
end

and tests passed.

[retgoat@iMac-Roman ~/temp/moviesmoviesmovies]$ git:(development) rspec spec/features/creating_movies_spec.rb
DEPRECATION WARNING: use_transactional_fixtures= is deprecated and will  be removed from Rails 5.1 (use use_transactional_tests= instead). (called  from <top (required)> at   /Users/retgoat/temp/moviesmoviesmovies/spec/features/creating_movies_spec.rb:3)
..

Finished in 0.73751 seconds (files took 1.91 seconds to load)
2 examples, 0 failures

Also I tried it in browser all went as expected — I saw validation errors.

Documentation is here

Also that post was useful.