admincontroller使用错误的帮助器来创建动作

时间:2012-03-21 22:33:33

标签: ruby-on-rails ruby-on-rails-3

我正在尝试通过我的管理员控制器创建操作来创建管理实例,但我不断收到错误消息:

ActiveRecord::RecordNotFound in AdminsController#show: Couldn't find User with id=4

跟踪表明它正在尝试使用会话助手(对于用户)而不是相应的adminsessions帮助程序。

app/helpers/sessions_helper.rb:20:in `current_user'
app/helpers/sessions_helper.rb:12:in `signed_in?'
app/views/layouts/application.html.erb:13:in  
app_views_layouts_application_html_erb__1013605049_93953830

我可以正确登录并创建管理员。我只是认为问题与我的管理员控制器中的redirect_to @admin有关,但我不确定。

如何设置它以便我的管理员控制器使用adminsessions帮助程序而不是会话帮助程序?任何帮助将不胜感激。

adminsessions_controller.rb

class AdminsessionsController < ApplicationController
  def new
    @title = "Log in"
  end

  def show
    @title = "Admin session"
  end  

  def create
    admin = Admin.authenticate(params[:adminsession][:email],
                               params[:adminsession][:password])
    if admin.nil?
      flash.now[:error] = "Invalid email/password combination."
      @title = "Log in"
      render 'new'
    else
      sign_in admin
      redirect_to admin
    end
  end

  def destroy
    sign_out
    redirect_to root_path                          
  end
end

admins_controller.rb

class AdminsController < ApplicationController

  def index
    @user = User.all
  end

  def show
    @admin = Admin.find(params[:id])
  end

  def new
    @admin = Admin.new
    @title = "New admin"
  end

  def create
     @admin = Admin.new(params[:admin])
    if @admin.save
      sign_in @admin
      flash[:success] = "Welcome admin!"
      redirect_to @admin
    else
      @title = "New admin"
      render 'new'
    end
  end
end

new.html.erb(我创建新用户的表单)

<div id="signupform_new">
   <%= form_for(@admin) do |f| %>
   <div class="field">
     <%= f.label :username %>
     <%= f.text_field :name, :class => "round"  %>
   </div>
   <div class="field">
     <%= f.label :email %>
     <%= f.text_field :email, :class => "round" %>
    </div>
    <div class="field">
      <%= f.label :password %>
      <%= f.password_field :password, :class => "round"  %>
    </div>
    <div class="field">
       <%= f.label :password_confirmation, "Confirmation" %>
       <%= f.password_field :password_confirmation, :class => "round"  %>
    </div>
    <div class="action">
      <%= button_tag "", :class => "acctSubmit" %>
    </div>
  <% end %>
</div>

sessions_helper.rb

module SessionsHelper

  def sign_in(user)
    session[:user_id] = user.id
    self.current_user = user
  end


  def signed_in?
   !current_user.nil?
  end

  def current_user=(user)
   @current_user = user
  end

  def current_user
   @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def current_user?(user)
   user == current_user
  end

 def authenticate
  deny_access unless signed_in?
 end    

 def sign_out
  session[:user_id] = nil
  self.current_user = nil
end

 def redirect_back_or(default)
  redirect_to(session[:return_to] || default)
  clear_return_to
end

def deny_access
  store_location
  redirect_to login_path, :notice => "Please log in to access this page."
end

private

  def store_location
    session[:return_to] = request.fullpath
  end

  def clear_return_to
    session[:return_to] = nil
  end
end

adminsessions_helper.rb

module AdminsessionsHelper


def sign_in(admin)
  adminsession[:admin_id] = admin.id
  self.current_admin = admin
end


def signed_in?
  !current_admin.nil?
end

def current_admin=(admin)
  @current_admin = admin
end

def current_admin
  @current_admin ||= Admin.find(adminsession[:admin_id]) if adminsession[:admin_id]
end

def current_admin?(admin)
  admin == current_admin
end

def authenticate
  deny_access unless signed_in?
end 

def sign_out
  adminsession[:admin_id] = nil
  self.current_admin = nil
end

def redirect_back_or(default)
  redirect_to(adminsession[:return_to] || default)
  clear_return_to
end

def deny_access
  store_location
 redirect_to login_path, :notice => "Please log in to access this page."
end

private

  def store_location
    adminsession[:return_to] = request.fullpath
  end

  def clear_return_to
    adminsession[:return_to] = nil
  end
end

1 个答案:

答案 0 :(得分:2)

所有控制器中都混合了所有助手(默认情况下)。看起来您正在使用的方法应该受到保护,或者您的控制器的私有成员。您可以在视图中使用辅助方法,即helper_method :signed_in?

就我个人而言,我从来都不喜欢与帮助者缺乏命名空间。我更喜欢演示者模式(参见RailsCasts Pro]。