Rails 4 form_for双嵌套注释 [英] Rails 4 form_for double nested comments
问题描述
我有三种模式。举例来说,我会称它们为树,树皮,雕刻(评论)。我试图为用户添加一个简单的表单部分以提交新评论(雕刻)。
以下设置会产生错误:
NoMethodError在/ trees / 1
未定义方法`tree'for nil:NilClass
控制器
class TreesController< ApplicationController
def show
@barks = Bark.where(tree_id:@ tree.id)
@ engraving = Engraving.new(:bark => @bark)
end
结束
类BarksController< ApplicationController
def show
@ engravin = Engraving.new(:bark => @bark)
@engravings = Engraving.where(bark_id:@ bark.id)
end
结束
类EngravingsController< ApplicationController
def show
@ bark = Bark.find(params [:bark_id])
@engraving = Engraving.new(params [:engraving])
end
结束
$ b $ def创建
@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),注意:'评论已成功创建。'}
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
end
模型
class Tree< ActiveRecord :: Base
belongs_to:user
has_many:barks
has_many:雕刻,通过:: barks
end
class Bark< ActiveRecord :: Base
belongs_to:用户
belongs_to:树
has_many:雕刻
代表:title,:link,:to => :tree,:prefix => true
def new_engraving_path
Rails.application.routes.url_helpers.new_tree_bark_engraving_path(book,self)
end
$ b $ 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 => :树皮,:前缀=> 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。在最后一个Engraving下,我希望有一个text_area表单来创建一个新的Engraving,并将其添加到这片树皮中。
#trees\show.html.erb
<%= render @barks%>
#barks\_bark.html.erb
<%=渲染部分:'barks / modal',当地人:{bark:bark}%>
#barks \modal.html.erb
<%渲染部分:'/ engravings / simpleForm',:collection => bark.engravings%>
#表格部分
<%= form_for @engraving,:url => (new_tree_bark_engraving_path(:tree_id => @ bark.tree,:bark_id => @ bark.id))do | f | %GT;
<%= f.hidden_field:bark_id%>
< div class =field>
<%= f.text_area:content%>
< / div>
< div class =actions>
<%= f.submit%>
< / div>
<%end%>
路线
getpages / home
资源:树做
集合做
获得'搜索'
发布'load_from_api'
结束
资源:树皮做
资源:雕刻
结束
结束
资源:树皮做
资源:雕刻
结束
roottrees#index
来源
您的错误消息 NoMethodError at / trees / 1 nil:NilClass
的未定义方法'tree'表示您必须调用某个地方 .tree
在 nil
对象上。我能看到发生的唯一情况是部分形式。我假设 @ bark.tree
失败。
从您的问题中不清楚您是哪个控制器正在渲染部分内容,但最可能的原因是 @bark
未被传递到表单中。确保你传递了 @bark
到你的部分,这里是一个例子:
<%=渲染部分:form,当地人:{bark:@bark}%>
First of all, I've found some very helpful answers already (see links below), but I'm still struggling to make this work. I haven't been programming for long, and this is definitely stretching what I feel comfortable claiming to understand.
I have three models. For example's sake, I'll call them Tree, Bark, Engraving (the comments). I'm trying to add a simple form partial for users to submit new comments (engravings).
The below setup is producing the error:
NoMethodError at /trees/1 undefined method `tree' for nil:NilClass
Controllers
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
end
Models
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
Calling the Form in a View
On the Tree show page, I have all the Bark populating there. Clicking on an Bark element opens a modal, where the Bark.content and Engravings are shown. Under the last Engraving, I want to have a text_area form to create a new Engraving, and add it to this "piece" of bark.
# 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 %>
Routes
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"
Other Answer Sources
Rails Routing Error for nested form_for
Twice Nested Resource Form_For Issue
form_for with nested resources
Your error message NoMethodError at /trees/1 undefined method 'tree' for nil:NilClass
means that somewhere you must be calling .tree
on a nil
object. The only place I can see that happening is in the form partial. I'm assuming that @bark.tree
is failing.
From your question it is not clear which controller you are rendering the partial from, but the most likely cause is that @bark
is not being passed into the form. Make sure that you are passing @bark
into your partial, here is an example:
<%= render partial: "form", locals: {bark: @bark} %>
这篇关于Rails 4 form_for双嵌套注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!