0

诚然,这个问题很长。因此,我感谢 ruby​​tutorial 社区的任何支持。我在第 9 章,尝试为登录用户创建会话。

我已经在 < Rails 3.1 中完成了教程。因为我现在使用的是 Rails 3.1,所以我继续阅读第 13 章并链接到(非常好的)Railscasts (#270) 关于这个主题。多亏了 has_secure_password,我能够很容易地重写我的用户注册。

当我尝试使用数据库中的用户登录时,我在控制台中看到了这个):

No route matches {:action=>"show", :controller=>"users"}

好像我需要创建一条路线,它应该可以工作。但如果是这样的话,为什么我可以去 'users/1' 并出现视图?我在会话和用户控制器(下)中使用路由 user_path(@user)、@user。

这就是我所做的。

将表单传递给 Session 控制器新操作(注意:我使用 form_tag 而不是 form_for)

    <%= form_tag sessions_path do %>

    <div class="field">
        <%= label_tag :email %><br />
        <%= text_field_tag :email, params[:email] %>
    </div>

    <div class="field">
        <%= label_tag :password %><br />
        <%= password_field_tag :password %>
    </div>

    <div class="actions">
        <%= submit_tag "Sign In" %>
    </div>
    <% end %>

然后,在 session_controller.rb 中创建动作

def create
#Assign object by email attribute
user = User.find_by_email(params[:email])
# User is present and has access, must be true otherwise nil object
if user && user.authenticate(params[:password])
    session[:user_id] = user.id
    RIGHT HERE IS THE PROBLEM
    redirect_to user_path(@user), :notice => "Logged in!"
else
    #Use flash.now on render not flash[]
    flash.now.alert = "Invalid email or password"
    render "new"
end
end

最后为 users_controller.rb 创建操作,效果很好。

def create
 @user = User.new(params[:user])
    if @user.save
        flash[:success] = "Welcome to the Sample App!"
        redirect_to @user
    else
        render "new"
        @user.password.clear
        @user.password_confirmation.clear
    end
end

离开我的用户模型:

attr_accessible :name, :email, :password, :password_confirmation

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

has_secure_password

哦,这是我当前的 routes.rb 文件

 resources :users
 resources :sessions, only: [:create, :new, :destroy]

 root to: "pages#home"
 match "/about",        to: "pages#about"
 match "/contact", to: "pages#contact"
 match "/help",         to: "pages#help"
 match "/signup",   to: "users#new" 
 match "/signin",   to: "sessions#new"
 match "/signout",  to: "sessions#destroy"

最后是我运行 rake 路由时的输出:

users GET    /users(.:format)          {:action=>"index", :controller=>"users"}
POST   /users(.:format)          {:action=>"create", :controller=>"users"}
new_user GET    /users/new(.:format)      {:action=>"new", :controller=>"users"}
edit_user GET    /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}
PUT    /users/:id(.:format)      {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format)      {:action=>"destroy", :controller=>"users"}
sessions POST   /sessions(.:format)       {:action=>"create", :controller=>"sessions"}
new_session GET    /sessions/new(.:format)   {:action=>"new", :controller=>"sessions"}
session DELETE /sessions/:id(.:format)   {:action=>"destroy", :controller=>"sessions"}
root        /                         {:controller=>"pages", :action=>"home"}
about        /about(.:format)          {:controller=>"pages", :action=>"about"}
contact        /contact(.:format)        {:controller=>"pages", :action=>"contact"}
help        /help(.:format)           {:controller=>"pages", :action=>"help"}
signup        /signup(.:format)         {:controller=>"users", :action=>"new"}
signin        /signin(.:format)         {:controller=>"sessions", :action=>"new"}
signout        /signout(.:format)        {:controller=>"sessions", :action=>"destroy"}
4

2 回答 2

0

这是您的redirect_to @user和/或 user_path(@user) 行。它们生成对 /users/:id 的请求,这显然不在您的 routes.rb 中。

于 2011-11-30T19:33:49.350 回答
0

@user在您的示例中为零 - 您正在分配给用户。rails 以一种不太有帮助的方式告诉您的是,如果没有用户,它就无法路由:controller => 'users', :action => 'show'

于 2012-01-20T23:06:03.763 回答