我想通過我的管理員控制器創建一個管理員實例創建行動,但我不斷收到一個錯誤,指出:admincontroller使用錯誤的幫手創建行動
ActiveRecord::RecordNotFound in AdminsController#show: Couldn't find User with id=4
跟蹤表明,它正試圖使用會話幫助程序(用戶)而不是相應的管理員幫助程序。
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
謝謝aceofspades。重命名方法以使它們獨特地解決了問題。我不應該假設每個幫助程序都是特定於其控制器的。 – gemmy 2012-03-22 21:01:42
如果你對於Rails @gemmy而言,再一次,這些應該是保護/私有控制器方法。在Rails中,每個地方都有一個地方,學習Rails意味着學習屬於哪個地方。 – aceofspades 2012-03-27 16:45:13