【问题标题】:Rails error message on views folder视图文件夹上的 Rails 错误消息
【发布时间】:2016-01-10 16:06:12
【问题描述】:

我已经经历了几个小时,我似乎无法找到问题所在。我在 rails 中做了一个简单的联系人表格,这是我正在遵循的教程,所以请耐心等待。我收到以下错误消息:

# 的未定义方法“名称”

我的数据库/迁移文件中有这个:

class CreateContacts < ActiveRecord::Migration
  def change
  create_table :contacts do |t|
    t.string :name
    t.string :email
    t.text :comments
    t.timestamps
    end
  end
end

这在我的contacts_controller.rb 文件中:

  class ContactsController < ApplicationController
    def new
      @contact = Contact.new
    end

    def create
    end
  end

这是在我的pages_controller.rb:

  class PagesController < ApplicationController
    def homepage
    end

    def about
    end
  end

这是我的html:

<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="well">
  <%= form_for @contact do |f| %>
    <div class="form-group">
      <%= f.label :name %>
      <%= f.text_field :name, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.label :email %>
      <%= f.email_field :email, class: 'form-control' %>
    </div>
    <div class="form-group">
      <%= f.label :comments %>
      <%= f.text_area :comments, class: 'form-control' %>
    </div>
    <%= f.submit 'Submit', class: 'btn btn-default' %>
  <% end %>
    </div>
  </div>
</div>

【问题讨论】:

  • 你在学习哪个教程?
  • @RichPeck 这是我购买的一门名为 CoderManual 的课程

标签: ruby-on-rails ruby methods undefined


【解决方案1】:

您是否进行了迁移?

如果没有,请这样做:

bundle exec rake db:migrate

这将在您的数据库中创建contacts 表(该表具有name 列以及您在构建表单时使用的其他列emailcomments)。另外,重新启动您的 Rails 服务器,然后您应该能够毫无问题地使用您的表单代码。

【讨论】:

  • 我确实使用了很多次,也使用了 db:drop ,但都没有成功。数据库中的表已经创建,我手动创建它。我没有使用 rails generate。
  • 如果您在 Rails 控制台中输入:Contact.firstContact.count,您会看到什么?
  • @K M Rakibul ,它现在可以工作了,我只是从头开始重新完成所有工作。
  • 不知何故,您的数据库中缺少 contacts 表,这就是您收到该错误的原因。很高兴知道您的问题现已解决。如果您发现它有用,请随时接受答案。要了解更多接受答案的工作原理,请参阅这篇文章:meta.stackexchange.com/questions/5234/…
【解决方案2】:

您可能尚未迁移您的表。

在您的控制台中运行:

rake db:migrate

然后重新启动您的服务器并重试。

【讨论】:

    【解决方案3】:

    其他答案总结得很好——你需要迁移你的contacts表:

    $ rake db:migrate
    

    你会想read up on Rails migrations here


    顺便说一句,

    页面

    我不知道每个人似乎都在关注哪个教程,它告诉您对 static 页面使用特定操作,这是 IMO 非常糟糕的做法(尤其是从长远来看)。

    更多更通用(和“Railsy*”)的解决方案是将数据的内容放入数据库中,然后您可以使用 model 调用它 - 允许您的所有页面都只有一个 show 视图:

    #config/routes.rb
    resources :pages, only: [:show]
    
    #app/controllers/pages_controller.rb
    class PagesController < ApplicationController
       def show
          @page = Page.find params[:id]
       end
    end
    
    #app/views/pages/show.html.erb
    <%= @page.title %>
    <%= @page.body %>
    

    您必须为 pages 数据表创建另一个迁移,如下所示:

    $ rails g migration CreatePages
    
    class CreatePages < ActiveRecord::Migration
      def change
      create_table :pages do |t|
        t.string :title
        t.text :body
        t.timestamps
        end
      end
    end
    
    $ rake db:migrate
    

    HTML

    您当前的 HTML 可以更有效率:

    <div class="row">
      <div class="col-md-4 col-md-offset-4">
        <div class="well">
          <%= form_for @contact do |f| %>
             <% attributes = {name: "text_field", email: "email_field", comments: "text_area"} %>
             <% attributes.each do |attr,val| %>
                <div class="form-group">
                   <%= f.label attr.to_sym %>
                   <%= f.send(val, attr.to_sym, class: 'form-control') %>
                </div>
             <% end %>
             <%= f.submit 'Submit', class: 'btn btn-default' %>
          <% end %>
        </div>
      </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多