Rails app说无法找到用户路由,应该正常工作

时间:2012-04-18 20:02:25

标签: ruby-on-rails rails-routing

我在使用自定义授权登录现有应用时遇到问题。当我登录时,我收到错误

No route matches {:action=>"show", :controller=>"users", :locale=>#<User id: 4, first_name: "asd", last_name: "asd", email: "c@c.com", password_digest: "$2a$10$FDkc/CkjbwS455r/s.OF9uXUzuRmzz3zsYEejij.0KCT...", access_level: 3, last_login

这是我的路线文件

Islasdelsol::Application.routes.draw do
  scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
    root :to => 'sites#index'
    get "logout" => "sessions#destroy", :as => "logout"
    get "login" => "sessions#new", as: "login"

    resources :sites, except: [:new, :show, :destroy, :create] do
      collection do
      get :about_us
      get :contact_us
      get :news
      get :reservations
      get :sell
      get :tour
    end
  end

resources :users do
  collection do
    get :monthly_letter
    get :admin
    get :regime
    get :general_assembly
    get :monthly_record
    get :newowner
    get :financial_report
    get :email_owners
  end
end

resources :email_owners do
  collection do
    get :email
    get :islas
    post :email
    post :islas
  end
end

resources :regimes do
  collection do
    get :spanish
    get :english
  end
end

resources :general_assemblies do
  collection do
    get :spanish
    get :english
  end
end
resources :sessions
resources :monthly_letters
resources :monthly_records


resources :financial_reports do
  collection do
    get :monthly
    get :yearly
  end
  end
  match "*path", to: "sites#not_found" # handles /en/fake/path/whatever
end
root to: redirect("/#{I18n.default_locale}") # handles /
match '*path', to: redirect("/#{I18n.default_locale}/%{path}")
end

这是我的会话控制器和表单,其中呼叫来自

class SessionsController < ApplicationController
  def create
  user = User.find_by_email(params[:email])
  if user && user.authenticate(params[:password])
    session[:user_id] = user.id
    redirect_to user, :notice => "Logged in!"
  else
    flash.now.alert = "Invalid email or password"
    render "new"
  end
end

表格

<%= form_tag sessions_path do %>
<table>
    <tr>
        <td><label for="email">Email</label></td>
    </tr>
    <tr>
        <td><%= text_field_tag :email, params[:email] %> </td>
    </tr>
    <tr>
        <td>Password</td>
    </tr>
    <tr>
        <td><label for="password"><%= password_field_tag :password %>   </label></td>
    </tr>
    <tr> 
        <td><%= submit_tag "Log in" %> </td>
    </tr>
</table>
<% end %>

def destroy
  session[:user_id] = nil
  redirect_to root_url, :notice => "Logged out!"
end

end

我的佣金路线控制器=用户

monthly_letter_users GET    /:locale/users/monthly_letter(.:format)   {:locale=>/en|es/, :action=>"monthly_letter", :controller=>"users"}
admin_users GET    /:locale/users/admin(.:format)            {:locale=>/en|es/, :action=>"admin", :controller=>"users"}
regime_users GET    /:locale/users/regime(.:format)           {:locale=>/en|es/, :action=>"regime", :controller=>"users"}
general_assembly_users GET    /:locale/users/general_assembly(.:format) {:locale=>/en|es/, :action=>"general_assembly", :controller=>"users"}
monthly_record_users GET    /:locale/users/monthly_record(.:format)   {:locale=>/en|es/, :action=>"monthly_record", :controller=>"users"}
newowner_users GET    /:locale/users/newowner(.:format)         {:locale=>/en|es/, :action=>"newowner", :controller=>"users"}
financial_report_users GET    /:locale/users/financial_report(.:format) {:locale=>/en|es/, :action=>"financial_report", :controller=>"users"}
email_owners_users GET    /:locale/users/email_owners(.:format)     {:locale=>/en|es/, :action=>"email_owners", :controller=>"users"}
users GET    /:locale/users(.:format)                  {:locale=>/en|es/, :action=>"index", :controller=>"users"}
POST   /:locale/users(.:format)                  {:locale=>/en|es/, :action=>"create", :controller=>"users"}
new_user GET    /:locale/users/new(.:format)              {:locale=>/en|es/, :action=>"new", :controller=>"users"}
edit_user GET    /:locale/users/:id/edit(.:format)         {:locale=>/en|es/, :action=>"edit", :controller=>"users"}
user GET    /:locale/users/:id(.:format)              {:locale=>/en|es/, :action=>"show", :controller=>"users"}
PUT    /:locale/users/:id(.:format)              {:locale=>/en|es/, :action=>"update", :controller=>"users"}
DELETE /:locale/users/:id(.:format)              {:locale=>/en|es/, :action=>"destroy", :controller=>"users"}

那么,谁能看到问题出在哪里?似乎有一条用户显示页面的路径...

修改:用户控制器

class UsersController < ApplicationController
  helper_method :sort_column, :sort_direction
  def index
    @owners = User.owners.search(params[:search]).order(sort_column + " " +      sort_direction).page(params[:page]).per_page(5)

  end

  def new
    @user = User.new
  end


  def show
    @user = User.find(params[:id])
    @monthly_letters = MonthlyLetter.recent
  end 

   def create
 @user = User.new(params[:user])
    if @user.save
      flash[:notice] = 'User Created!'
      redirect_to @user
    else
      flash[:alert] = "User has not been created."
     render "index"
   end 
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy
    flash[:notice] = "User has been deleted."
    redirect_to current_user
  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 monthly_letter
    @monthly_letter = MonthlyLetter.new
  end 
end

1 个答案:

答案 0 :(得分:3)

这里的问题是url_for不知道如何处理:locale参数。

您可以告诉错误中发生了什么,因为它说:locale参数是一个对象#&lt; User&gt;。 它应该是一个像其他参数一样的字符串。 这通常意味着您使用不正确或不完整的参数调用link_to / url_for。

解决此问题的第一步可能是为:locale参数设置默认值 在你的应用程序控制器Rails Guide's i18n document为此提供了指导 以及查看HTTP标头和参数的更完整的解决方案。