0

我一直跟着兩個老的和修訂railscasts & this的東西,我必須沿着相同的路線使樓調查5

我跟着它高達一些點,但也不是所顯示的問題在表單上也沒有增加答案。以下是我的模型代碼

answers.rb

class Answer < ActiveRecord::Base 
    attr_accessor :content, :question_id 
    belongs_to :question 
end 

surveys.rb

class Survey < ApplicationRecord 
    attr_accessor :name, :questions_attributes 
    has_many :questions 
    accepts_nested_attributes_for :questions, allow_destroy: true 
end 

questions.rb

class Question < ApplicationRecord 
    attr_accessor :content, :survey_id, :answers_attributes 
    belongs_to :survey 
    has_many :answers 
    accepts_nested_attributes_for :answers, allow_destroy: true 
end 

調查控制器

class SurveysController < ApplicationController 
    before_action :set_survey, only: [:show, :edit, :update, :destroy] 

    # GET /surveys 
    # GET /surveys.json 
    def index 
    @surveys = Survey.all 
    end 

    # GET /surveys/1 
    # GET /surveys/1.json 
    def show 
    end 

    # GET /surveys/new 
    def new 
    @survey = Survey.new 
    3.times do 
     question = @survey.questions.build 
     4.times { question.answers.build } 
    end 
    end 

private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_survey 
     @survey = Survey.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def survey_params 
     params.require(:survey).permit(:name, :question_id) 
    end 
end 

瀏覽

_form.html.erb

<%= f.fields_for :questions do |builder| %> 
    <%= render 'question_fields', f: builder %> 
<% end %> 
<%= link_to_add_fields "Add Question", f, :questions %> 

_question_fields.html.erb

<fieldset> 
    <%= f.label :content, "Question" %><br /> 
    <%= f.text_area :content %><br /> 
    <%= f.check_box :_destroy %> 
    <%= f.label :_destroy, "Remove Question" %> 
    <%= f.fields_for :answers do |builder| %> 
    <%= render 'answer_fields', f: builder %> 
    <% end %> 
    <%= link_to_add_fields "Add Answer", f, :answers %> 
</fieldset> 

_answers_fields.html.erb

<p> 
    <%= f.label :content, "Answer" %> 
    <%= f.text_field :content %> 
    <%= f.check_box :_destroy %> 
    <%= f.label :_destroy, "Remove" %> 
</p> 

show.html.erb

<p id="notice"><%= notice %></p> 

<p> 
    <strong>Name:</strong> 
    <%= @survey.name %> 
</p> 

<ol> 
    <% for question in @survey.questions %> 
    <li><%= h question.content %></li> 
    <% end %> 
</ol> 

<p> 
    <%= link_to "Edit", edit_survey_path(@survey) %> | 
    <%= link_to "Destroy", @survey, :confirm => 'Are you sure?', :method => :delete %> | 
    <%= link_to "View All", surveys_path %> 
</p> 

遷移

class CreateSurveys < ActiveRecord::Migration[5.0] 
    def change 
    create_table :surveys do |t| 
     t.string :name 

     t.timestamps 
    end 
    end 
end 

class CreateQuestions < ActiveRecord::Migration[5.0] 
    def change 
    create_table :questions do |t| 
     t.string :survey_id 
     t.string :integer 
     t.text :content 

     t.timestamps 
    end 
    end 
end 

還有什麼我缺少的是需要不要在軌道5,我已經在這個小時,它仍然困惑我爲什麼它告訴我這個錯誤 - 表'app.answers'不存在當我從嵌套形式調用答案。任何在這方面的幫助將非常感激。

+0

您可以發佈您的調查視圖表單嗎?它看起來像你可能期待nested_attributes,但你沒有在你的'permit'調用中將它們白名單列出,所以想要仔細檢查你的表單是否設置正確 –

+0

主要問題出現在調查控制器的新方法中,這個特定行 - 4times {question.answers.build} –

+0

哦,所以你甚至沒有看到,好吧...你有運行遷移和一切,對嗎? –

回答

1

這裏的主要問題是它看起來像你忘了'回答'遷移來創建表,創建並運行它,並應該修復。

此外,這些attr_accessor調用將會搞砸了。他們在老版本的Rails中是必需的,但不再是現在,現在只是拋棄了。例如

隨着attr_accessor代碼

post = Post.new(title: "Something") 
#=> #<Post id: nil, title: nil, created_at: nil, updated_at: nil> 
post.title = "Something" 
#=> "Something" 
puts post 
#=> #<Post id: nil, title: nil, created_at: nil, updated_at: nil> 

沒有

post = Post.new(title: "Something") 
#=> #<Post id: nil, title: "Something", created_at: nil, updated_at: nil> 
post.title = "Something Else" 
#=> "Something Else" 
puts post 
#=> #<Post id: nil, title: "Something Else", created_at: nil, updated_at: nil> 

正如你所看到的,第一塊,在我的Post模型有attr_accessortitle屬性,什麼也沒有按預期工作;我無法更新標題。一旦我刪除它,事情就開始按照他們應該的方式工作。

基於聊天討論,你_form.html.erb缺少form_for標籤,並且應該看起來像

<%= form_for @survey do |f| %> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    <!-- your current code here --> 
<% end %> 

你有_answers_field.html.erb_question_fields.html.erb呼籲

<%= render 'answer_fields', f: builder %> 

通知,複數/單數不匹配。

,最後,在你的控制器,你是不是允許它應該結束了看上去像(除非我錯了)嵌套屬性PARAMS

def survey_params 
    params.require(:survey).permit(:name, :question_attributes: [:id, :content, :_destroy, answer_attributes: [:id, :content, :_destroy]) 
end 

近一從聊天問題是,該協會需要inverse_of,因爲belongs_to默認情況下需要在rails 5中。最後一個小問題是,Answer目前正在繼承ActiveRecord :: Base和其他模型ApplicationRecord

+0

即使我註釋掉了attr_accessor,它也顯示了同樣的問題。更新了視圖代碼。 –

+0

如果你是像我這樣的rails 5的新手,還需要在inverse_of上讀一下。更多關於它的地方 - http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html –