0

看來我無法弄清楚如何在Michael Hartl railstutorial示例應用程序的導航欄菜單上顯示「登錄」按鈕。Railstutorial缺少登錄標頭按鈕

我目前在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> 

的application.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 . 

的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'   
    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 

authemtication_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: "[email protected]" 
    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('[email protected]') } 

    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) { "[email protected]" } 
    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 

的Gemfile

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' 

我可能會錯過一些必要的文件。請讓我知道我還可以幫助任何可能提供幫助的人。

謝謝!

回答

3

你會討厭這個,但它很簡單。

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

將會把無論是從代碼到您的HTML返回,而

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

將簡單地執行代碼,而不是顯示它。所以你需要使用<%= %>而不是<% %>

只需添加等號!

UPDATE

至於你的下拉列表不顯示,一旦你退出,你是明確告訴它不通過把它顯示在循環的部分內登錄時顯示:

<% 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 %> 
+0

我非常感謝你解決這個問題。你也幫我理解了代碼..布拉沃! – tkin1

+0

沒有足夠的代表投票了,但如果別人會這樣會很好。謝謝!! – tkin1

+0

我認爲我發現的是,在缺少下拉菜單時,登錄時爲 。點擊帳戶後可查看 下拉欄。點擊任何#鏈接後,賬戶下拉欄無法再工作......在重新加載頁面或點擊配置文件後,它會再次運行,直到點擊後不再出現爲止。 再次感謝您解決<%= %> – tkin1