简体   繁体   中英

Can not get data from the database

How to display values from a database on a page for display, I do not try how much I get this error.

undefined method `email' for nil:NilClass

development.erb.html

<% @development.each do |developer| %>
  <p><% @developer.email %></p>
<% end %>

welcome_controller.rb

class WelcomeController < ApplicationController
  def index
    @development = TeamMember.all
  end
end

model.rb

class TeamMember < ApplicationRecord
end

db schema.rb

create_table "team_members", force: :cascade do |t|
  t.string "email"
  t.string "first_name"
  t.string "last_name"
  t.string "team_text"
  t.string "image_url"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

它应该是<%= developer.email %>而不是<% @developer.email %>

尝试将欢迎文件夹内的文件名更改为 index.html.erb 而不是 development.erb.html

You should change your .erb file to:

<% @development.each do |developer| %>
  <p><%= developer.email %></p>
<% end %>

When you use <% %> to run ruby code inline, it won't output the result (ideal for .each), however when you want the resulting value (the developer's email) to be output into the HTML, you should use <%= %> .

In addition, the file should be found at /views/welcome/index.erb.html

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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