Rails app saying can't find users route, should be working

放肆的年华 提交于 2019-12-25 05:03:49

问题


I have having a problem logging into my existing app with custom authorization. When I login, I get the error

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

Here is my routes file

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

Here is my sessions controller and form, where the call is originating from

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

<%= 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

And my rake routes controller=users

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"}

So, can anyone see where the problem is? There appears to be a route to users show page...

Edit: Users controller

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:


The problem here is that url_for doesn't know how to deal with the :locale parameter.

You can tell what is happening in the error because it says :locale parameter is an object #<User>. It should be a string like the other parameters. That generally means that your link_to/url_for is called with incorrect or incomplete parameters.

The first step to solving this is probably to set a default for the :locale parameter in your application controller. Rails Guide's i18n document provides guidance for that as well as more complete solutions of looking at HTTP headers and parameters.



来源:https://stackoverflow.com/questions/10217093/rails-app-saying-cant-find-users-route-should-be-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!