0

似乎我无法弄清楚如何在 Michael Hartl railstutorial Sample App 的导航栏菜单上显示“登录”按钮。

我目前在 9.3 并且测试失败。我试着回顾每个不同的章节,找出我在哪里丢失了登录按钮或如何......我已经重写了很多代码,希望能解决这个问题。无论如何,这是我拥有的代码。

我还注意到,在以登录用户身份注销后,我的下拉菜单不会继续显示。

无论如何..

我的 _header.html.erb

 <header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
  <%= link_to "sample app", 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", 'users_path' %></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", 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>

和我的 application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
   <%= stylesheet_link_tag    "application", media: "all" %>
   <%= javascript_include_tag "application" %>
   <%= csrf_meta_tags %>
   <%= render 'layouts/shim' %>    
 </head>
 <body>
   <%= render 'layouts/header' %>
   <div class="container">
     <% flash.each do |key, value| %>
       <div class="alert alert-<%= key %>"><%= value %></div>
     <% end %>
     <%= yield %>
     <%= render 'layouts/footer' %>
     <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
 </html>

应用程序.js

// This is a manifest file that'll be compiled into application.js, which will includeall the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .

路线.rb

SampleApp::Application.routes.draw do

  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  root to: 'static_pages#home'              

  match '/signup',  to:'users#new'          
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: 'delete'
  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'  


end

authemication_pages_spec.rb

 require 'spec_helper'

 describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: full_title('Sign in')) }
  end

  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
        before{ click_button "Sign in" }

        it { should have_selector('title', text: full_title('Sign in')) }
        it { should have_selector('div.alert.alert-error', text: 'Invalid')}

        describe "after visiting another page" do
            before { click_link "Home" }
            it { should_not have_selector ('div.alert.alert-error') }
        end
    end



     describe "valid information" do
        let (:user) { FactoryGirl.create(:user) }
        before { sign_in user }


         it { should have_selector('title',         text: full_title(user.name)) }
        it { should have_link('Profile',          href: user_path(user)) }
      it { should have_link('Settings',     href: edit_user_path(user)) }
        it { should have_link('Sign out',         href: signout_path) }
        it { should_not have_link('Sign in',    href: signin_path) }

      describe "followed by signout" do
        before { click_link "Sign out" }
        it { should have_link('Sign in',    href: signin_path) }
      end
    end
    describe "authorization" do

       describe "for non-signed-in users" do
        let(:user) { FactoryGirl.create(:user) }

        describe "in the Users controller" do
          before { visit edit_user_path(user) }
          it { should have_selector('title', text: full_title('Sign in')) }
        end

        describe "submitting to the update action" do
          before { patch user_path (user) }
          specify { expect(response_.to redirect_to(signin_path)) }
        end
      end
    end
  end
end

users_controller.rb

 require 'spec_helper'

 describe "User pages" do

   subject { page }

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

it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }

结尾

  describe "signup page" do
before { visit signup_path }

it { should have_selector('h1',    text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }

结尾

  describe "signup" do

before { visit signup_path }

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

describe "with valid information" do

  before do
    fill_in "Name",         with: "Example User"
    fill_in "Email",        with: "user@example.com"
    fill_in "Password",     with: "foobar"
    fill_in "Confirmation", with: "foobar"
  end

  it "should create a user" do
     expect { click_button submit }.to change(User, :count).by(1)
   end

  describe "after saving the user" do
    before { click_button submit }
    let(:user) { User.find_by_email('user@example.com') }

    it { should have_link('Sign out') }
    it { should have_selector('title', text: user.name) }
    it { should have_selector('div.alert.alert-success', text: 'Welcome') }
  end
end
  end

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

describe "page" do
  it { should have_content("Update your profile") }
  it { should have_selector('title', text: full_title('Edit user'))}
  it { should have_link('change', href: 'http://gravatar.com/emails') }
end

describe "with invalid information" do
  before { click_button "Save changes" }

  it { should have_content('error') }
end

describe "with valid information" do
  let(:new_name)  { "New Name" }
  let(:new_email) { "new@example.com" }
  before do
    fill_in "Name",         with: new_name 
    fill_in "Email",        with: new_email
    fill_in "Password",     with: user.password
    fill_in "Confirmation", with: user.password
    click_button "Save changes"
  end

  it { should have_selector('title', text: new_name) }
  it { should have_selector('div.alert.alert-success') }
  it { should have_link('Sign out', href: signout_path) }
  specify { expect(user.reload.name).to   eq new_name}
  specify { expect(user.reload.email).to   eq new_email}
    end
  end
end

宝石文件

source 'https://rubygems.org'
# ruby '2.0.0'
# Commented out , on version of Ruby 1.9.3
gem 'rails', '3.2.13'
gem 'bootstrap-sass', '2.1'
gem 'bcrypt-ruby', '3.0.1'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.5'
  gem 'coffee-rails', '~> 3.2.2'
  gem 'uglifier', '1.2.3'
 end

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

gem 'turbolinks', '1.3.0'
gem 'jquery-rails', '2.0.2'

group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '4.2.1'
end

group :production do
gem 'pg', '0.12.2'
end

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

我可能错过了一些必要的文件。请让我知道我还能如何帮助任何可能提供帮助的人。

谢谢!!

4

1 回答 1

3

你会讨厌这个,但它真的很简单。

<%= link_to "Sign in", signin_path %>

将从代码返回的任何内容放入您的 HTML 中,而

<% link_to "Sign in", signin_path %> # (note the lack of an equals sign)

将简单地执行代码而不显示它。

所以你需要使用<%= %>而不是<% %>.

只需添加一个等号!

更新

至于您的下拉菜单在您退出后未显示,您通过将其放在登录时显示的循环部分中来明确告诉它不要显示:

<% if signed_in? %>
  # Show this code when signed in (this is where your dropdown is)
<% else %>
  # Show this code when not signed in (this is where your login button is)
<% end %>
于 2013-10-23T04:25:37.647 回答