未定义的方法`current_user?'错误

时间:2016-02-11 17:28:28

标签: ruby-on-rails-4 devise

Rails的新手,并试图弄清楚为什么我在尝试查看索引属性页时收到上述错误消息。我正在使用Devise和CanCanCan。代码如下。如果遗漏的内容有助于发现问题,请告诉我。

查看:

<% @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可以在应用程序的任何地方使用,基于Devise文档将其用作帮助程序。

1 个答案:

答案 0 :(得分:1)

current_user可以在应用中的任何位置使用。 current_user?不是Devise提供的方法。