2014-11-21 93 views
0

我在第一次認真地嘗試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 | 
+----+------------+---------+---------------------+---------------------+ 

我想怎麼辦是顯示問題1 10,如上所述,在legal_forms的顯示視圖中。

爲此,我已經設置了我的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,我面臨以下錯誤:

ActiveRecord::StatementInvalid in LegalForms#show Mysql2::Error: Unknown column 'questions.legal_form_id' in 'where clause': SELECT questions .* FROM questions WHERE questions . legal_form_id = 1

錯誤消息將循環中的問題(在給出sql錯誤的情況下有意義)引腳。我清楚地明白了這裏的一些錯誤想法。任何幫助正確的軌道將不勝感激。

回答

1

顯然,您的專欄名稱存在問題。

您的專欄名稱是legalform_id,而您請求legal_form_id

我不知道你如何定義你的問題模型,但那裏有什麼可疑的東西。

+0

幹得好,先生! – neanderslob 2014-11-21 00:19:14

1

如果您在模型中使用的has_many協會 - 默認的foreing關鍵是model_name_id 您需要foreing_key添加到您的has_many assotiation爲legalform_id或數據庫重命名列legal_form_id