2016-02-11 117 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