2016-04-28 25 views
1

我一直在尋找其他答案,我仍然得到這個錯誤與params失蹤。我猜這是一個回形針問題,但通過搜索文檔後,我找不到解決此問題的任何內容。失敗/錯誤:click_button「添加電影」ActionController :: ParameterMissing:參數丟失或值爲空:電影

我的特色規格

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 

我的電影模型

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:照片,匹配:[?/ PNG \ Z /,/ JPE克\ Z /]```

我的控制器

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 

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

錯誤

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

Github如果neccessary。

+0

如何是你的form_for像 – uzaif

+0

'''<%=的form_for(@movie)%> <%= f.label:標題%> <%= f.text_field:標題,類:「形式控制',佔位符:'電影標題',自動對焦:true%> <%= f.label:synopsis%> <%= f.text_area:synopsis,rows:10,class:'form-control',佔位符: '簡介'%> <%= f.label:photo%> <%= f.file_field:photo,class:「btn btn-primary btn-sl pull-left」%>''' – kyle

+0

更新時發生錯誤電影? – uzaif

回答

0

我認爲這是因爲您在strong_params中使用require

我改變你的代碼

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

,並通過測試。

[[email protected] ~/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 

另外我在瀏覽器中試過所有按預期去 - 我看到驗證錯誤。

文檔是here

而且that職位是有益的。

+0

太棒了!非常感謝你! retgoat – kyle

+0

歡迎,@kyle :) – retgoat

相關問題