2015-11-01 77 views
0

我添加了彈性搜索gem並在導航欄上添加了一個搜索按鈕,但是當我想要用戶搜索帖子時,它會要求我登錄或註冊。我希望用戶能夠查看帖子的索引頁面,並在posts_controller文件中添加了before_filter,但我不知道爲什麼它會一直要求我註冊或登錄。以下是我的代碼:Ruby on Rails:彈性搜索顯示所有帖子

posts_controller.rb

class PostsController < ApplicationController 
    before_action :set_post, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! , except: [:index,:show] 
    before_filter :check_user, only: [:edit,:update,:destroy] 



    # GET /posts 
    # GET /posts.json 

    def search 
    if params[:search].present? 
    @posts = Post.search(params[:search]) 
    else 
    @posts = Post.all 
    end 
    end 

    def index 
    if params[:tag] 
     @posts = Post.tagged_with(params[:tag]) 
    else 
     @posts = Post.all 
    end 
    end 

    # GET /posts/1 
    # GET /posts/1.json 
    def show 
    @reviews = Review.where(post_id: @post.id) 
    end 

    # GET /posts/new 
    def new 
    @post = Post.new 
    end 


    # GET /posts/1/edit 
    def edit 
    @post = Post.find(params[:id]) 
    end 

    # POST /posts 
    # POST /posts.json 
    def create 
    @post = Post.new(post_params) 
    @post.user_id = current_user.id 

    respond_to do |format| 
     if @post.save 
     format.html { redirect_to @post, notice: 'Post was successfully created.' } 
     format.json { render :show, status: :created, location: @post } 
     else 
     format.html { render :new } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /posts/1 
    # PATCH/PUT /posts/1.json 
    def update 
    respond_to do |format| 
     if @post.update(post_params) 
     format.html { redirect_to root_url, notice: 'Post was successfully updated.' } 
     format.json { render :show, status: :ok, location: @post } 
     else 
     format.html { render :edit } 
     format.json { render json: @post.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /posts/1 
    # DELETE /posts/1.json 
    def destroy 
    @post.destroy 
    respond_to do |format| 
     format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_post 
     @post = Post.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def post_params 
     params.require(:post).permit(:title, :description,:image,:all_tags) 
    end 

    def check_user 
     if current_user.id != @post.user_id 
     redirect_to root_path , alert: "Sorry this Post belongs to someone else" 
    end 
    end 

end 
+0

您是否可能在application_controller中調用'authenticate_user!'?此外,你應該堅持寧靜的路線,並在索引行動中進行搜索。您是否嘗試過使用'binding.pry'來查看它的位置? – user3162553

回答

0

您可以使用

skip_before_filter :authenticate_user! , :only => [:index,:show] 
在控制器

跳過爲這些行動的before_filter。

0

我能夠通過在before_action後驗證中添加搜索方法來解決問題。代碼如下:

before_action :authenticate_user! , except: [:index,:show,:search] 
相關問題