2013-12-12 45 views
0

我是Ruby on Rails的新手,已經淘汰了Stackoverflow和互聯網以盡我所能,並且仍然難倒了。Rails4 Simple_form嵌套表單字段不顯示

在我的設置中,評分屬於產品和has_many評論。我正在使用簡單表單,並嘗試使用嵌套fields_for通過評級表單添加RatingComment。有趣的是,當使用單數形式:rating_comment時,顯示該字段。但正如預期的那樣,我在嘗試保存時遇到了未經許可的參數錯誤。當我使用複數:rating_comments時,該字段消失。這與SO posting類似,但將@ rating.rating_comments.build添加到新動作仍然不適用於我。我試過重新啓動服務器多次,甚至重置數據庫無濟於事。希望得到任何幫助,因爲過去幾天我一直在解決這個問題。

注:我也拿出了我認爲是不相關的代碼從下面的代碼片段。如果我需要顯示更多信息,請讓我知道。提前致謝!

的routes.rb

resources :ratings, only: [:new, :create, :edit, :update, :destroy] do 
    resources :rating_comments, shallow: true 
end 

rating.rb

class Rating < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :user 

    has_many :rating_comments, foreign_key: "rating_id", dependent: :destroy  
    accepts_nested_attributes_for :rating_comments, reject_if: :all_blank 
end 

rating_comment.rb

class RatingComment < ActiveRecord::Base 
    belongs_to :rating 
    validates :rating_id, presence: true 
end 

ratings_controller.rb

class RatingsController < ApplicationController 
    before_action :signed_in_user, only: [:create, :new, :show] 

    def new 
    @product = Product.find(params[:id]) 
    @rating = @product.ratings.new 
    @rating.rating_comments.build 
    end 

    def create 
    @product = Product.find(params[:product_id]) 
    @rating = @product.ratings.build(rating_params) 
    @rating.user_id = current_user.id 
    ... 

    private 
    def rating_params 
     params.require(:rating).permit(:user_id, :product_id, :rating, rating_comments_attributes: [:rating_id, :content]) 
    end 
end 

評價/ _new_rating_form.html.erb

<%= simple_form_for([@product, @rating], html: { class: 'form-horizontal' }) do |f| %> 
    <%= f.error_notification %> 
    <%= f.input :rating, collection: 1..10, as: :radio_buttons, 
      item_wrapper_class: 'inline', checked: 5 %> 
    <%= f.simple_fields_for :rating_comments do |rc| %> 
    <fieldset> 
     <%= rc.input :content, label: "Comments" %> 
    </fieldset> 
    <% end %> 
    <%= f.error :base %> 
    <%= f.button :submit, "Submit" %> 
<% end %> 

回答

0

很不好意思地承認,事實證明,我錯寫成行動的看法。一旦我把它放入new.html,它終於奏效了。