2017-01-31 2 views
0

問題はレールに「強いパラメータ」があることです。 画像をアップロードするにはトンボを使用します。paramが見つからないか、値が空です:gallery

空のフォームを送信すると、エラー処理のパラメータが取得できないという問題があります。理由は何でしょうか?

コントローラー:

方法データベースに画像を保存し、写真付きのページにユーザーを送信する「作成」もまだあります。

def index 
    @gallery = Gallery.new 
    @galleries = Gallery.all 
end 

def create 
    @gallery = Gallery.new(gallery_params) 

    if @gallery.save 
    redirect_to galleries_path, flash: { success: 'Your Image was successfully save.' } 
    else 
    redirect_to :back,   flash: { alert: "Your Image don't save." } 
    end 
end 

def gallery_params 
    params.require(:gallery).permit(:image) 
end 

再生回数:

= form_for @gallery do |f| 
    = f.file_field :image 
    = f.submit 'Submit', class: 'btn bth-primary btn-lg' 

パラメータ:

{"utf8"=>"✓", "authenticity_token"=>"8eotQtkj8SElqJLdHuOX8r+dWrCJRWTmVcyfd1mSLD/8MjWw/ElH/HCxZFSJ6oOWaxpbLbn4kAg5nlFycsgjHg==", "commit"=>"Submit"} 
+0

「エラー処理のパラメータがありません」とはどういう意味ですか? –

+1

あなたが得ているものは、完璧に上質で、期待通りです。 –

+0

妥当性チェックエラーが発生しないという場合は、おそらく 'image'フィールドが原因である可能性があります。 'Gallery'モデルを投稿する必要があります –

答えて

1

これは正常な動作です、私は通常、これらの例に何ActionController::Parameters#require

のためのマニュアルを参照してくださいキャッチされます例外を表示し、フラッシュメッセージを表示してユーザーに通知します。手動でモデルにエラーを追加することもできます。

def create 
    @gallery = Gallery.new(gallery_params) 

    if @gallery.save 
    redirect_to galleries_path, flash: { success: 'Your Image was successfully save.' } 
    else 
    redirect_to :back, flash: { alert: "Your Image don't save." } 
    end 
rescue ActionController::ParameterMissing => e 
    redirect_to :back, flash: { alert: "Please attach an image." } 
end 
関連する問題