2012-04-05 81 views
0

我目前正在遵循Ruby on Rails教程,並在完成本教程中的section 8.3後遇到輕微問題。在功能上,一切工作正常,我能夠通過我的形式和一切添加用戶。但是,現在我的表單在網站的每個頁面上都顯示了兩次。我多次瀏覽了代碼,卻無法弄清楚是什麼導致了這種情況發生。這裏是我的Heroku的網站的鏈接,你可以看到這個問題:http://deep-mist-5284.heroku.com/Michael Hartl的Ruby on Rails教程:第8.3章 - 網站表單顯示兩次

當我運行軌道服務器的網站它輸出,它是使用這些文件,但我已經通過這些看上去並沒有發現什麼錯誤。

Started GET "/signup" for 127.0.0.1 at 2012-04-05 16:43:13 -0700 
Processing by UsersController#new as HTML 
Rendered shared/_error_messages.html.erb (1.2ms) 
Rendered layouts/_stylesheets.html.erb (17.9ms) 
Rendered layouts/_header.html.erb (10.6ms) 
Rendered layouts/_footer.html.erb (2.6ms) 
Rendered users/new.html.erb within layouts/application (280.3ms) 

有沒有人知道是什麼原因導致了這個問題?

UsersController 類UsersController < ApplicationController中

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

    def new 
    @user = User.new 
    @title = "Sign up" 
    end 

    def create 
    @user = User.new(params[:user]) 
    if @user.save 
     flash[:success] = "Welcome to the Sample App!" 
     redirect_to @user 
    else 
     @user.password = "" 
     @user.password_confirmation = "" 
     @title = "Sign up" 
     render 'new' 
    end 
    end 
end 

共享/ _error_messages.html.erb

<% if @user.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@user.errors.count, "error") %> 
     prohibited this user from being saved:</h2> 
    <p>There were problems with the following fields:</p> 
    <ul> 
    <% @user.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
<% end %> 

佈局/ _stylesheets.html.erb

<!--[if lt IE 9]> 
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> 
<![endif]--> 
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %> 
<%= stylesheet_link_tag 'blueprint/print', :media => 'print' %> 
<!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]--> 
<%= stylesheet_link_tag 'custom', :media => 'screen' %> 

佈局/ _header.html .erb

<header> 
    <%= link_to logo, root_path %> 
    <nav class="round"> 
    <ul> 
     <li><%= link_to "Home", root_path %></li> 
     <li><%= link_to "Help", help_path %></li> 
     <li><%= link_to "Sign in", '#' %></li> 
    </ul> 
    </nav> 
</header> 

佈局/ _footer.html.erb

<footer> 
    <nav class="round"> 
    <ul> 
     <li><%= link_to "About", about_path %></li> 
     <li><%= link_to "Contact", contact_path %></li> 
     <li><a href="http://news.railstutorial.org/">News</a></li> 
     <li><a href="http://www.railstutorial.org/">Rails Tutorial</a></li> 
    </ul> 
    </nav> 
</footer> 

用戶/ new.html.erb

<h1>Sign up</h1> 

<%= form_for(@user) do |f| %> 
    <%= render 'shared/error_messages' %> 
    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :email %><br /> 
    <%= f.text_field :email %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %><br /> 
    <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
    <%= f.label :password_confirmation, "Confirmation" %><br /> 
    <%= f.password_field :password_confirmation %> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Sign up" %> 
    </div> 
<% end %> 

佈局/應用

<!DOCTYPE html> 
<html> 
    <head> 
    <title><%= title %></title> 
    <%= csrf_meta_tag %> 
    <%= render 'layouts/stylesheets' %> 
    </head> 
    <body> 
    <div class="container"> 
     <%= render 'layouts/header' %> 
     <section class="round"> 
     <% flash.each do |key, value| %> 
      <%= content_tag(:div, value, :class => "flash #{key}") %> 
     <% end %> 
     <%= yield %> 
     </section> 
     <section class="round"> 
     <%= yield %> 
     </section> 
     <%= render 'layouts/footer' %> 
     <%= debug(params) if Rails.env.development? %> 
    </div> 
    </body> 
</html> 

回答

2

你有兩個<% =你的佈局/ application.html.erb中的yield%。你有Rails將users/new.html.erb(和其他每一頁)的內容放入layouts/application.html.erb中兩次。刪除一個,所有將被修復。

+0

哇,非常感謝你!!!!那樣做了! – h4xhouse 2012-04-06 02:24:34

相關問題