我想要在show.html.erb中獲取像這樣的數據,但它不起作用。 如何從點表中獲取數據?Rails-如何爲已創建表創建視圖
這裏是代碼。
show.html.erb
<% @planaction.each do |action| %>
<hr>
<%= action.spot.name %>
<%= action.spot.description %>
<hr>
<%= action.title %>
<%= action.experience %>
<% end %>
plan.rb
class Plan < ActiveRecord::Base
has_many :plan_actions
end
plan_action.rb
class PlanAction < ActiveRecord::Base
belongs_to :plan
has_one :spot
end
spot.rb
class Spot < ActiveRecord::Base
belongs_to :plan_action
end
plan_actions_controller.erb
class PlanPagesController < ApplicationController
def show
@plan = Plan.find(params[:id])
@planaction = @plan.plan_actions
end
end
這裏的錯誤消息
未定義的方法'名稱」的零:NilClass
和H ere是現貨表的遷移文件。
class CreateSpots < ActiveRecord::Migration
def change
create_table :spots do |t|
t.integer :spot_id
t.integer :plan_action_id
t.string :name
t.text :description
t.time :time_open
t.time :time_close
t.date :dayoff
t.string :address
t.integer :tel
t.string :image
t.string :image2
t.string :image3
t.timestamps null: false
end
end
end
代碼看起來沒問題,你會得到什麼錯誤? –
請解釋不行嗎? – Pavan
這裏是一個錯誤未定義的方法'name'爲零:NilClass – vanhiro