在rails教程第9章中找不到id = edit的用户

时间:2013-12-27 17:34:48

标签: ruby-on-rails

我是铁杆新手。我正在关注Michael Hartl的铁路教程书。在第9章中,当我尝试编辑用户配置文件时,它将无法工作。 / user / 4 / edit呈现编辑页面 但是当我点击应该呈现编辑页面的用户配置文件中的设置时,它会被路由到/ user // edit 并显示以下错误:

在UsersController#show中的ActiveRecord :: RecordNotFound 找不到具有id = edit

的用户

我知道这将是一个小错误,但我无法弄明白。所以,谢谢你提前给予任何帮助。

这是我的文件

users_controller.rb

class UsersController < ApplicationController

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

def new
@user=User.new
end

def create 
@user = User.new(params[:user])
if @user.save
  sign_in @user
  flash[:success] = "Welcome!"
  redirect_to @user
else
  render 'new'
end
end

def edit
@user = User.find(params[:id])
end

def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
  flash[:success] = "Profile updated"
  sign_in @user
  redirect_to @user
else
 render 'edit'
end
end

end

_header.html.erb

<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
  <%= link_to "project",root_path, id: "logo" %>    
<nav>
  <ul class="nav pull-right">
    <li><%= link_to "Home",    root_path %></li>
    <li><%= link_to "Help",    help_path %></li>

        <% if signed_in? %>
          <li><%= link_to "Users", '#' %></li>
          <li id="fat-menu" class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">
              Account <b class="caret"></b>
            </a>
            <ul class="dropdown-menu">
              <li><%= link_to "Profile", user_path(current_user) %></li>
              <li><%= link_to "Settings", edit_user_path(@current_user) %></li>
              <li class="divider"></li>
              <li>
                <%= link_to "Sign out", signout_path, method: "delete" %>
              </li>
            </ul>
          </li>

    <% else %>
      <li><%= link_to "Sign in", signin_path%></li>
    <% end %>

  </ul>
</nav>
 </div>
</div>
</header>

sessions_helper.rb

module SessionsHelper

def sign_in(user)
  cookies.permanent[:remember_token] = user.remember_token
  self.current_user = user
end

def signed_in?
  !current_user.nil?
end

def current_user=(user)
  @current_user = user
end

def current_user
  @current_user ||= User.find_by_remember_token(cookies[:remember_token])
end

def sign_out
  self.current_user = nil
  cookies.delete(:remember_token)
end

end

edit.html.erb

<% provide(:title,'Edit user') %>
<h1>Update your profile</h1>

<div class="row">
  <div class="span6 offset3">

    <%= form_for (@user) do |f| %>
    <%= render 'shared/error_messages' %>
    <%= f.label :name %>
    <%= f.text_field :name %>

    <%= f.label :email %>
    <%= f.text_field :email %>

    <%= f.label :password %>
    <%= f.password_field :password %>

    <%= f.label :password_confirmation, "Confirm password" %>
    <%= f.password_field :password_confirmation %>

    <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>

  <% end %>

  <%= gravatar_for @user %>
  <a href="http://gravatar.com/emails" target="_blank"> Change </a>

  </div>
</div>

的routes.rb

SampleApp::Application.routes.draw do

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

root to: 'static_pages#home'

match '/signup',   to: 'users#new', via: [:get, :post]
match '/signin',   to: 'sessions#new', via: [:get]
match '/signout',  to: 'sessions#destroy', via: [:delete]

match '/help',    to: 'static_pages#help', via: [:get, :post]
match '/contact', to: 'static_pages#contact', via: [:get, :post]
match '/about',   to: 'static_pages#about', via: [:get, :post]
end

sessions_controller.rb

class SessionsController < ApplicationController

def new
end

def create
  user = User.find_by_email(params[:session][:email].downcase)
  if user && user.authenticate(params[:session][:password])
    sign_in user
    redirect_to user
  else
    flash.now[:error] = "Invalid email/password combination"
    render 'new'
  end
end

def destroy
  sign_out
  redirect_to root_path
end

end

1 个答案:

答案 0 :(得分:3)

更改此

<li><%= link_to "Settings", edit_user_path(@current_user) %></li>

到这个

<li><%= link_to "Settings", edit_user_path(current_user) %></li>

您只能在用于呈现标题的控制器方法中使用您有权访问的变量。您的show控制器仅声明@user,而不是@current_user,因此在呈现标头时,创建的网址对user_id没有任何内容。如果使用current_user,系统会调用current_user中的SessionsHelper方法,系统会返回当前用户,并且您的网址中应包含user_id

以下是Hartl教程示例应用程序的github帐户。能够通过它并将其与您自己的代码进行比较并找到您偏离的位置可能是一种好习惯。可能会有一些不同的东西,因为你还没有完成教程,但你已经足够了,它应该大致完全相同:https://github.com/mhartl/sample_app