首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Michael Hartl的Ruby on Rails教程:第8.3章-网站表单显示两次

Michael Hartl的Ruby on Rails教程:第8.3章-网站表单显示两次
EN

Stack Overflow用户
提问于 2012-04-06 07:59:05
回答 1查看 1K关注 0票数 1

我目前正在学习Ruby on Rails教程,在完成教程中的section 8.3之后遇到了一个小问题。在功能上,一切都很好,我可以通过我的表单和一切添加用户。然而,现在我的表单在网站的每个页面上都显示了两次。我已经多次检查了代码,就是找不出是什么导致了这种情况的发生。这是我的Heroku网站的链接,你可以在那里看到这个问题:http://deep-mist-5284.heroku.com/

当我在rails服务器中运行网站时,它会输出它正在使用的这些文件,但我已经查看了这些文件,没有发现任何错误。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
  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

代码语言:javascript
运行
复制
<% 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 %>

布局/_Layouts heets.html.erb

代码语言:javascript
运行
复制
<!--[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

代码语言:javascript
运行
复制
<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

代码语言:javascript
运行
复制
<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>

users/new.html.erb

代码语言:javascript
运行
复制
<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 %>

布局/应用

代码语言:javascript
运行
复制
<!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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-06 08:27:44

在您的<%= /application.html.erb中有两个%> your布局。您让Rails将users/new.html.erb (以及每隔一个页面)的内容放入您的布局/application.html.erb中两次。删除其中一个,所有的都将被修复。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10037651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档