【发布时间】:2011-07-19 11:20:25
【问题描述】:
我在视图上收到以下错误消息:http://localhost:3000/microposts/2 SQLite3::SQLException:没有这样的列:answers.micropost_id: SELECT "answers".* FROM "answers" WHERE ("answers".micropost_id = 2)
这是我的迁移:
| db > 迁移 > 创建微博 |
class CreateMicroposts < ActiveRecord::Migration
def self.up
create_table :microposts do |t|
t.string :content
t.timestamps
end
end
def self.down
drop_table :microposts
end
end
| db > 迁移 > 创建答案 |
class CreateAnswers < ActiveRecord::Migration
def self.up
create_table :answers do |t|
t.string :location
t.text :body
t.references :micropost
t.integer :micropost_id
t.timestamps
end
end
def self.down
drop_table :answers
end
end
答案控制者:
def create
@answer = Answer.new(params[:answer])
@answer.micropost = @micropost; @answer.save && @micropost.save
redirect_to micropost_path(@micropost)
respond_to do |format|
if @answer.save
format.html { redirect_to(@answer, :notice => 'Answer was successfully created.') }
format.xml { render :xml => @answer, :status => :created, :location => @answer }
else
format.html { render :action => "new" }
format.xml { render :xml => @answer.errors, :status => :unprocessable_entity }
end
end
end
和视图:
<p id="notice"><%= notice %></p>
<p>
<b>Content:</b>
<%= @micropost.content %>
<h2>Location Answers:</h2>
<% @micropost.answers.each do |answer| %>
<p>
<b>Answer:</b>
<%= answer.body%>
</p>
<%end %>
<h2> Answer a location:</h2>
<%= form_for ([@micropost, @micropost.answers.build]) do |f| %>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :body %><br/>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</p>
<%= link_to 'Edit', edit_micropost_path(@micropost) %> |
<%= link_to 'Back', microposts_path %>
我找不到这个应用程序有什么问题。 - 我试图回滚并再次迁移它没有工作。 - 我试图在迁移中手动添加“t.integer:micropost_id”,但它不起作用 我的模型有关联“belongs_to”和“has_many”,我添加了“ 资源:微博做 资源:答案 结尾 到我的 config.rb 文件中。
【问题讨论】:
标签: ruby-on-rails sqlite comments associations