我在第一次认真尝试使用rails应用程序并且遇到了一些麻烦。
我有两个模型,一个名为LegalForm
,另一个名为Question
。一个包含不同表单的列表,另一个包含与每个表单相关的问题。或者,正如我在legal_form.rb
class LegalForm < ActiveRecord::Base
has_many :questions
end
每个数据库表的设置如下:
mysql> select * from questions;
+----+--------------+-----------------+---------------+---------------------------------------------------------------------+---------------------+---------------------+
| id | legalform_id | question_number | question_type | the_question | created_at | updated_at |
+----+--------------+-----------------+---------------+---------------------------------------------------------------------+---------------------+---------------------+
| 1 | 1 | 1 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 2 | 1 | 2 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 3 | 1 | 3 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 4 | 1 | 4 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 5 | 1 | 5 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 6 | 1 | 6 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 7 | 1 | 7 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 8 | 1 | 8 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 9 | 1 | 9 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
| 10 | 1 | 10 | lorem | lorem ipsum dolor sit amet consectetuer adipiscing elit proin risus | 2014-11-20 21:27:04 | 2014-11-20 21:27:04 |
+----+--------------+-----------------+---------------+---------------------------------------------------------------------+---------------------+---------------------+
10 rows in set (0.00 sec)
mysql> select * from legal_forms;
+----+------------+---------+---------------------+---------------------+
| id | title | company | created_at | updated_at |
+----+------------+---------+---------------------+---------------------+
| 1 | First_Form | 1 | 2014-11-20 20:58:53 | 2014-11-20 20:58:53 |
+----+------------+---------+---------------------+---------------------+
我想做什么是在legal_forms的show view上显示问题1-10,如上所述。
为此,我已按如下方式设置legal_forms_controller.rb
:
class LegalFormsController < ApplicationController
def index
@legal_forms=LegalForm.all
end
def ufilter
end
def own
end
def show
@legalform = LegalForm.find(params[:id])
@questions = @legalform.questions
end
private
def legal_forms_params
params.require(:legalform).permit(:title, :company)
end
end
并设置了我的show.html.erb
文件:
<h1>LegalForms#show</h1>
<p>Find me in app/views/legal_forms/show.html.erb</p>
<% @questions.each do |question| %>
<span><%= question.the_question %> </span>
<% end %>
但是,当我导航到localhost:3000/legal_forms/1
的页面时,我遇到了以下错误:
ActiveForcord :: StatementInvalid在LegalForms#show中 Mysql2 ::错误:未知栏&#39; questions.legal_form_id&#39;在&#39; where子句&#39;:SELECT
questions
。* FROMquestions
WHEREquestions
。legal_form_id
= 1
错误消息将问题固定在循环上(考虑到sql错误,这是有意义的)。我在这里明确对某些事情有错误的想法。任何有助于走上正轨的人都将不胜感激。
答案 0 :(得分:1)
您的专栏名称明显存在问题。
您的列名称为legalform_id
,您请求legal_form_id
。
我不知道你如何定义你的问题模型,但那里有一些可疑的东西。
答案 1 :(得分:1)
如果在模型中使用has_many关联 - 默认情况下,foreing键是model_name_id
您需要将foreing_key添加到has_many assotiation legalform_id
或将数据库中的列重命名为legal_form_id