質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

4762閲覧

before_actionのexcept optionが機能しない

innjera

総合スコア132

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2016/11/05 05:13

Rails 5.0.0.1で開発しています。

以下の通り、before_actionを設定しており、indexアクションでは適用外にしたいのですが、コードを記載しても添付写真のエラーが出てしまいます。before_action :signin_required, except: [:index, :show]を消すと、動作に問題はありません。

解決方法をご教示頂けますと幸甚です。

ruby

1class User::ProductsController < User::Base 2 before_action :signin_required, except: [:index, :show] 3 4 def index 5 if params[:user_id] 6 @user = User.find(params[:user_id]) 7 @products = @user.products 8 else 9 @products = Product.all.order("id") 10 end 11 end 12 13 def search 14 @user = current_user 15 @products = Product.search(params[:q]) 16 render "index" 17 end 18 19 def show 20 @product = Product.find(params[:id]) 21 end 22 23 def new 24 @product = Product.new(posted_at: Time.current) 25 end 26 27 def edit 28 @product = current_user.products.find(params[:id]) 29 end 30 31 def create 32 @product = Product.new(product_params) 33 @product.user = current_user 34 if @product.save 35 redirect_to [current_user, :user_products] 36 else 37 render "edit" 38 end 39 end 40 41 def update 42 @product = current_user.products.find(params[:id]) 43 @product.assign_attributes(product_params) 44 if @product.save 45 redirect_to [current_user, :user_products] 46 else 47 render "edit" 48 end 49 end 50 51 def like 52 @product = Product.find(params[:id]) 53 current_user.voted_products << @product 54 redirect_to [current_user, :user_product], notice: "投票しました" 55 end 56 57 def unlike 58 current_user.voted_products.destroy(Product.find(params[:id])) 59 redirect_to :voted_user_products, notice: "削除しました。" 60 end 61 62 def voted 63 @products = current_user.voted_products.order("votes.created_at DESC") 64 end 65 66 private 67 def product_params 68 params.require(:product).permit(:price, :brand, :memo, :dispatch, :posted_at) 69 end 70end 71

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

エラーは search メソッドで起きているようですが except: [:index, :show] なので正しいように見えます。
index メソッドでも起きるのでしょうか?

投稿2016/11/05 07:11

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

innjera

2016/11/05 08:10

コメント有難う御座います。ご指摘の通り、searchをexceptに加えたところ解決しました! 検索画面(searchメソッド)をindexメソッドのviewに入れていた為、indexメソッドで起きているエラーかと勘違いしておりました。エラー画面をよく見るとsearchで起きているエラーと明記されていますね。。。初歩的なことですいませんでした。
退会済みユーザー

退会済みユーザー

2016/11/05 09:52

解決できて良かったですね :)。ハマってると気づかないことって多いですよね〜
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問