访问edit_user_path时,访问9.3故障的Michael Hartl教程

时间:2014-05-03 16:50:17

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 railstutorial.org

我有一个我无法解决的错误。我按照教程中的步骤操作,但我无法将测试传递给绿色。

我收到了这个错误:

$ bundle exec rspec spec/requests/user_pages_spec.rb -e "Edit page"

Run options: include {:full_description=>/Edit\ page/}
FFF

Failures:

  1) User pages Edit page
     Failure/Error: before { visit edit_user_path(user) }
     ActionView::Template::Error:
       undefined method `model_name' for NilClass:Class
     # ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb__878999397_98066850'
     # ./spec/requests/user_pages_spec.rb:49:in `block (3 levels) in <top (required)>'

  2) User pages Edit page
     Failure/Error: before { visit edit_user_path(user) }
     ActionView::Template::Error:
       undefined method `model_name' for NilClass:Class
     # ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb__878999397_98066850'
     # ./spec/requests/user_pages_spec.rb:49:in `block (3 levels) in <top (required)>'

  3) User pages Edit page
     Failure/Error: before { visit edit_user_path(user) }
     ActionView::Template::Error:
       undefined method `model_name' for NilClass:Class
     # ./app/views/users/edit.html.erb:6:in `_app_views_users_edit_html_erb__878999397_98066850'
     # ./spec/requests/user_pages_spec.rb:49:in `block (3 levels) in <top (required)>'

Finished in 0.37836 seconds
3 examples, 3 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:52 # User pages Edit page
rspec ./spec/requests/user_pages_spec.rb:54 # User pages Edit page
rspec ./spec/requests/user_pages_spec.rb:53 # User pages Edit page

我的route.rb文件中是否缺少某些内容?我不明白该怎么做。

edit.html.erb

<% provide(:title, "Editer") %>
<h1>Mise a jour du profil</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">changer</a>
    </div>
</div>

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] = "Bienvenue sur mon site"
        redirect_to @user
    else
        render 'new'
    end
end

private 
    def user_params
        params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end

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

user_pages_spec.rb

describe "Edit" do
        let(:user) { FactoryGirl.create(:user) }
        before { visit edit_user_path(user) }

        describe "page" do
            it { should have_content("Mise a jour du profil") }
            it { should have_title("Editer") }
            it { should have_link("changer",  href: 'http://gravatar.com/emails') }
        end

        describe "Avec info invalide" do
            before { click_button "Sauver" }
            it { should have_content('erreur') }
        end
    end

的routes.rb

  root to: 'static_pages#home'
  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'

  # views/users
  resources :users
  match '/signup', to: 'users#new'

  # views/sessions
  resources :sessions, only: [:new, :create, :destroy]
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete

当我在导航器中运行adresse:/ users / 1 / edit

有一个错误:

NoMethodError in Users#edit

Showing /home/tprails/RubyOnRails/new_app/app/views/users/edit.html.erb where line #6 raised:

undefined method `model_name' for NilClass:Class
Extracted source (around line #6):

3: 
4: <div class="row">
5:     <div class="span6 offset3">
6:       <%= form_for(@user) do |f| %>
7:         <%= render 'shared/error_messages' %>
8: 
9:         <%= f.label :name %>

我的route.rb文件中是否缺少某些内容?我不明白该怎么做。

感谢您的帮助,对不起我的英语不好

1 个答案:

答案 0 :(得分:5)

您的编辑方法位于您的私有区块中,将其移到私有区域上方,它应该可以正常工作。