简体   繁体   English

如何在Rails中调试NoMethodError

[英]how to debug a NoMethodError in rails

I'm following the jumpstartlabs blogger tutorial and I'm down to section i5 and struggling with getting a NoMethodError in Authors#new 我正在关注jumpstartlabs博主教程 ,现在进入第i5节,努力在Authors#new中获取NoMethodError

Here is the stack trace: 这是堆栈跟踪:

NoMethodError in Authors#new
Showing /home/.../blogger/app/views/authors/_form.html.erb where line #16 raised:

undefined method `username' for #<Author:0xb6379068>
Extracted source (around line #16):

  <div class="field">
      <%= f.label :username %><br>
      <%= f.text_field :username %>
  </div>
  <div class="field">
    <%= f.label :email %><br>

Trace of template inclusion: app/views/authors/new.html.erb

The new.html.erb is really simple new.html.erb非常简单

<h1>New Author</h1>

<%= render 'form' %>

<%= link_to 'Back', authors_path %>

And here is my _form.html.erb 这是我的_form.html.erb

<%= form_for(@author) do |f| %>
  <% if @author.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@author.errors.count, "error") %> prohibited this author from being saved:</h2>

      <ul>
      <% @author.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
      <%= f.label :username %><br>
      <%= f.text_field :username %>
  </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 %><br />
  <%= f.password_field :password_confirmation %>
</div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

I really want to know what I can do in the rails console to debug this. 我真的很想知道我可以在Rails控制台中做什么来调试它。 Or how to read the trace to figure out why this is occuring. 或如何读取跟踪以找出发生这种情况的原因。 I can attach more of the full trace if needed but it's pretty long. 如果需要,我可以附加更多完整的跟踪信息,但是它很长。

The rails error message is clear enough. rails错误消息已经很清楚了。 It says that the instance of Author class doesn't respond to username . 它说Author类的实例不响应username Ensure you have defined your data model correctly and upplied all database migrations (you must have a username field in authors table). 确保正确定义了数据模型并提供了所有数据库迁移的权限(在authors表中必须具有用户名字段)。 Run rails console ( rails c ) and try Author.new.username to check, or Author.new.attributes to list defined attributes. 运行Rails控制台( rails c ),并尝试Author.new.username检查,或Author.new.attributes列出定义的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM