用户编辑和删除路由提供错误,命名空间路由

时间:2012-04-20 04:40:51

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

我的路线有问题,我认为这是因为params被错误地传递了。每当我尝试进入用户编辑页面时,我都会收到以下错误:

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 =&gt; #User ID(哈希),但我不确定这是不是问题。如果是,我不知道为什么它将用户哈希作为:locale?这是我的一些应用代码

它抱怨的代码:
应用程序/视图/管理/用户/ 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

并在我的应用程序视图中 应用程序/视图/布局/ application.html.erb

<%= link_to_unless_current "English", locale: "en" %> |
<%= link_to_unless_current "Spanish", locale: "es" %> 

并不是默认为EN?

2 个答案:

答案 0 :(得分:1)

将此添加到我的应用程序控制器,如Ryan Bates中指定的railscast修复它。

def default_url_options(options = {})
  {locale: I18n.locale}
end

答案 1 :(得分:0)

您没有传递语言环境参数
试试edit_admin_user_path(owner, :locale => "en")