找不到字段First Capybara Element Not Found

时间:2014-01-26 18:20:07

标签: ruby-on-rails

我又回来了另一个问题。我正在研究Michael Hartl rails教程,我似乎陷入了这个部分(我已经用版本控制重做了三次,我在这里得到了相同的结果。)

我收到此错误消息:

2)用有效信息编辑用户页面      失败/错误:fill_in“First”,带:new_first      水豚:: ElementNotFound:        无法找到字段“First”      './spec/requests/user_pages_spec.rb:57:in'块(4级)in'

我多次得到同样的信息。我认为此时我有大约7个错误。

所以,这是相关的代码:

require 'spec_helper'

describe "User pages" do

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }
  end

  describe "signup page" do

    before { visit signup_path }

    let(:submit) { "Create my account" }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end
    end

    describe "with valid information" do
      before do
        fill_in "First",        with: "Example"
        fill_in "Last",         with: "User"
        fill_in "Email",        with: "user@example.com"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
        expect do
          click_button "Create my account"
        end.to change(User, :count).by(1)
      end
    end
  end

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

    describe "page" do

      specify { page { should have_content("Update your profile") } }
      specify { page { should have_title("Edit User") } }
      specify { page { should have_link('change', href: 'http://gravatar.com/emails') } }
    end

    describe "with invalid information" do
      before { click_button "Save changes" }
      specify { page { should have_content("error") } }
    end

    describe "with valid information" do
      let(:new_first)  { "First" }
      let(:new_last)  { "Last" }
      let(:new_email) { "new@andover.edu" }
      before do
        fill_in "First",             with: new_first
        fill_in "Last",             with: new_last
        fill_in "Email",            with: new_email
        fill_in "Password",         with: user.password
        fill_in "Confirm Password", with: user.password
        click_button "Save changes"
      end

      specify { page { should have_selector("div.alert.alert-success") } }
      specify { page { should have_title("new_first") } }
      specify { page { should have_link('Sign out', href: signout_path) } }
      specify { expect(user.reload.name).to  eq new_first }
      specify { expect(user.reload.name).to  eq new_last }
      specify { expect(user.reload.email).to eq new_email }
    end
  end
end

这是我的user_pages规范。这是我的用户控制器。

class UsersController < ApplicationController
  before_action :signed_in_user, only: [:edit, :update]
    def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      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(user_params)
      # Handle a successful update.
    else
      render 'edit'
    end
  end

  private

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

    # Before filters

    def signed_in_user
      redirect_to signin_url, notice: "Please sign in." unless signed_in?
    end
end

这是html页面本身。

<% 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 :first %>
      <%= f.text_field :first %>

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

      <%= 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">change</a>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

我在密码字段中遇到了同样的错误。 我添加了&#34; 用户_ &#34;到有错误的字段来修复它。

describe "with valid information" do
    before do
     fill_in "Name",           with: "Example User"
     fill_in "Email",          with: "user@example.com"
     fill_in "user_password",  with: "foobar"
     fill_in "Confirmation",   with: "foobar"
end