我的路线有问题,我认为这是由于参数传递不正确。每当我尝试进入用户编辑页面时,都会收到以下错误:
No route matches {:action=>"edit", :controller=>"admin/users", :locale=>#<User id: 10, first_name: "Chase", last_name: "Jensen", email: "email@email.com", password_digest: "$2a$10$xTvG5gP73w2/nVW5jD.BSO3Z46S5v9oGogmfjo3gsGHf...", access_level: 2, last_login: nil, auth_token: nil, created_at: "2012-04-20 02:19:33", updated_at: "2012-04-20 02:19:33">}
我注意到它说 :locale=>#User ID (hash) 但我不确定这是否是问题所在。如果是,我不知道为什么将用户哈希作为:locale?这是我的一些应用程序代码
它抱怨的代码:
app/views/admin/users/index.html.erb
<% @owners.each do |owner| %>
<%= owner.first_name %>
<%= link_to "Edit Owner", edit_admin_user_path(owner) %>
<%= link_to "Delete Owner", admin_user_path(owner), :method => :delete, :confirm => "Are you sure you want to delete this owner?" %>
<% end %>
用户控制器
class Admin::UsersController < Admin::BaseController
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:notice] = "User has been updated."
redirect_to @user
else
flash[:alert] = "User has not been updated."
render :action => "edit"
end
end
def index
@owners = User.all
end
end
routes.rb(缩短)
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
namespace :admin do
resources :users do
collection do
get :newowner
end
end
end
end
rake 路由输出
newowner_admin_users GET /:locale/admin/users/newowner(.:format) {:locale=>/en|es/, :action=>"newowner", :controller=>"admin/users"}
admin_users GET /:locale/admin/users(.:format) {:locale=>/en|es/, :action=>"index", :controller=>"admin/users"}
POST /:locale/admin/users(.:format) {:locale=>/en|es/, :action=>"create", :controller=>"admin/users"}
new_admin_user GET /:locale/admin/users/new(.:format) {:locale=>/en|es/, :action=>"new", :controller=>"admin/users"}
edit_admin_user GET /:locale/admin/users/:id/edit(.:format) {:locale=>/en|es/, :action=>"edit", :controller=>"admin/users"}
admin_user GET /:locale/admin/users/:id(.:format) {:locale=>/en|es/, :action=>"show", :controller=>"admin/users"}
PUT /:locale/admin/users/:id(.:format) {:locale=>/en|es/, :action=>"update", :controller=>"admin/users"}
DELETE /:locale/admin/users/:id(.:format) {:locale=>/en|es/, :action=>"destroy", :controller=>"admin/users"}
当路线似乎匹配时,我很困惑为什么会收到此错误。有人对此有任何见解吗?提前致谢。
编辑:
此时应在应用程序控制器中设置区域设置
before_filter :set_locale
private
def set_locale
I18n.locale = params[:locale] if params[:locale].present?
end
并在我的应用程序视图中 app/views/layouts/application.html.erb
<%= link_to_unless_current "English", locale: "en" %> |
<%= link_to_unless_current "Spanish", locale: "es" %>
它不是默认为EN吗?