2016-02-11 142 views
0

新手入門,並試圖找出當我嘗試查看我的索引屬性頁時,爲什麼會收到上述錯誤消息。我正在使用Devise和CanCanCan。代碼如下。請讓我知道是否缺少有助於發現問題的信息。未定義的方法`current_user?'錯誤

查看:

<% @properties.each do |property| %> 
    <div class = "row for-sale-header"> 
     <div class="col-xs-8"> 
      <% if user_signed_in? && current_user? %> 
       <%= link_to edit_property_path(property) do %> 
        <i class="glyphicon glyphicon-edit" aria-hidden="true"></i> Edit 
       <% end %><br /> 
      <% end %> 
      <h5>Property ID: <%= property.id %> <br /> </h5> 
      <%= property.description %> 
     </div> 
    </div> 
    <br /> 
<% end %> 

控制器:

class PropertiesController < ApplicationController 

before_action :authenticate_user!, except: [:index] 
before_action :set_user, only: [:show, :edit, :update] 

def index 
    @properties = Property.all 
end 

def new 
    @property = current_user.properties.new 
end 

def create 
    @property = current_user.properties.new(property_params) 
    respond_to do |format| 
     if @property.save 
     format.html { redirect_to @property, notice: "Property was successfully created." } 
     format.json { render :show, location: @property } 
     else 
     format.html { render :new } 
     format.json 
     end 
    end 
end 

def update 

    respond_to do |format| 
     if @property.update(property_params) 
     format.html { redirect_to @property, notice: "You've successfully updated your property listing!" } 
     format.json { render :show, status: :ok, location: @property } 
     else 
     format.html { render :edit } 
     format.json { render json: @property.errors, status: :unprocessable_entity } 
     end 
    end 
end 

private 

def property_params 
     params.require(:property).permit(:street, :city, :province, :postal_code, :description, :picture) 
end 

def set_user 
    @property = Property.find(params[:id]) 
end 

end 

應用控制器:

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    before_action :configure_permitted_parameters, if:   
:devise_controller? 

protected 

    def configure_permitted_parameters 
devise_parameter_sanitizer.for(:sign_up) << :name 
    end 
    end 

我想current_user可以隨時隨地根據設計文檔的應用程序中使用,使用它作爲幫手。

回答

1

current_user可以用在應用程序的任何地方。 current_user?不是由Devise提供的方法。

+0

謝謝。這幫助我找到正確的道路來找出正確的答案。 – dgreen22