1
更新頭像我要測試的這款update
行動:試驗中的RSpec
def update
@player = Player.find(params[:id])
begin
@player.update_attributes(player_params_avatar)
flash[:success] = "Avatar updated"
redirect_to player_path(@player)
rescue ActionController::ParameterMissing => e
flash[:danger] = "First choose avatar file"
redirect_to :back
end
end
def player_params_avatar
params.require(:player).permit(:avatar)
end
我想是這樣的:
describe "PUT #update" do
let!(:player1) { FactoryGirl.create :player }
context "when data is valid" do
it "update avatar" do
avatar = File.new(Rails.root + 'spec/factories/images/1.png')
put :update, player: { id: player1.id, avatar: avatar }
expect(flash[:success]).to be_present
end
end
但RSpec的控制檯我得到的錯誤:
Failure/Error: put :update, player: { id: player1.id, avatar: avatar } ActionController::UrlGenerationError: No route matches {:action=>"update", :controller=>"players", :player=>{:id=>"1", :avatar=>"#<File:0xaed5b84>"}}
爲什麼這段代碼會出現這個錯誤?有特定的球員id
所以應該也是正確的網址例如。
編輯:我使用回形針寶石的化身。
我routes
文件:
Rails.application.routes.draw do
get 'pages/about'
get 'matches/new'
root 'pages#home'
resources :players
resources :matches
get '*path' => redirect('/')
end
路由是否正確設置? – apneadiving
是的,在瀏覽器中我可以更新頭像,一切都OK。 – Jensky