首先,我已經找到了一些非常有幫助的答案(請參閱下面的鏈接),但我仍然努力做到這一點。我沒有長時間的編程,而且這絕對是我舒服自稱理解的東西。Rails 4 form_for雙重嵌套註釋
我有三種模式。舉例來說,我會稱它們爲樹,樹皮,雕刻(評論)。我正在嘗試爲用戶添加一個簡單的表單部分以提交新評論(雕刻)。
的下面設置是製造誤差:
NoMethodError在/樹木/ 1 未定義的方法`樹」的零:NilClass
控制器
class TreesController < ApplicationController
def show
@barks = Bark.where(tree_id: @tree.id)
@engraving= Engraving.new(:bark=> @bark)
end
end
class BarksController < ApplicationController
def show
@engravin= Engraving.new(:bark=> @bark)
@engravings = Engraving.where(bark_id: @bark.id)
end
end
class EngravingsController < ApplicationController
def show
@bark= Bark.find(params[:bark_id])
@engraving = Engraving.new(params[:engraving])
end
end
def create
@bark = Bark.find(params[:bark_id])
@engraving = Engraving.new(params[:engraving])
@engraving.user_id = current_user.id
respond_to do |format|
if @engraving.save
format.html { redirect_to tree_bark_path(@tree, @bark), notice: 'Comment was successfully created.' }
format.json { render action: 'show', status: :created, location: @engraving}
else
format.html { render action: 'new' }
format.json { render json: @engraving.errors, status: :unprocessable_entity }
end
end
端
模型
class Tree < ActiveRecord::Base
belongs_to :user
has_many :barks
has_many :engravings, through: :barks
end
class Bark< ActiveRecord::Base
belongs_to :user
belongs_to :tree
has_many :engravings
delegate :title, :link, :to => :tree, :prefix => true
def new_engraving_path
Rails.application.routes.url_helpers.new_tree_bark_engraving_path(book, self)
end
def serializable_hash(*args)
super.merge 'new_engraving_path' => new_engraving_path
end
end
class Engraving< ActiveRecord::Base
belongs_to :user
belongs_to :bark
delegate :tree, :to => :bark, :prefix => true
def to_edit_path
Rails.application.routes.url_helpers.edit_tree_bark_engraving_path bark_tree, bark, self
end
def to_path
Rails.application.routes.url_helpers.tree_bark_engraving_path bark_tree, bark, self
end
def serializable_hash(*args)
super.merge 'path' => to_path, 'edit_path' => to_edit_path
end
end
在視圖中調用窗體
在樹顯示頁面上,我將所有樹皮填充到那裏。點擊一個Bark元素打開一個模式,顯示Bark.content和Engravings。在最後的雕刻下,我想要一個text_area表單來創建一個新的雕刻,並將其添加到這片「樹皮」中。
# trees\show.html.erb
<%= render @barks %>
# barks\_bark.html.erb
<%= render partial: 'barks/modal', locals: { bark: bark} %>
# barks\modal.html.erb
<% render partial: '/engravings/simpleForm', :collection => bark.engravings %>
# Form partial
<%= form_for @engraving, :url => (new_tree_bark_engraving_path(:tree_id => @bark.tree, :bark_id => @bark.id)) do |f| %>
<%= f.hidden_field :bark_id %>
<div class="field">
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
路線
get "pages/home"
resources :trees do
collection do
get 'search'
post 'load_from_api'
end
resources :barks do
resources :engravings
end
end
resources :barks do
resources :engravings
end
root "trees#index"
對方的回答來源
Rails Routing Error for nested form_for
Twice Nested Resource Form_For Issue
form_for with nested resources
謝謝!變量現在正在傳入。 – user1573016 2014-10-02 22:12:49
表單現在顯示多次。我檢查了部分電話,但事實並非如此。表格數量與之前提交的評論數量相同。爲什麼評論的數量會決定要顯示多少表格? – user1573016 2014-10-03 00:14:20
問題在於部分調用中的:collection。現在全部修好! – user1573016 2014-10-03 00:32:55