Ruby on rails 钢轨缺钢
您好,我想在/notes/show_html.slim中显示@detail,但我有一个错误缺少部分细节/_detail,我如何在/notes/show.html.slim中输出@detail routes.rb 详细信息\u controller.rb /details/show.html.slimRuby on rails 钢轨缺钢,ruby-on-rails,ruby,Ruby On Rails,Ruby,您好,我想在/notes/show_html.slim中显示@detail,但我有一个错误缺少部分细节/_detail,我如何在/notes/show.html.slim中输出@detail routes.rb 详细信息\u controller.rb /details/show.html.slim #note #post_content .show h1 = @note.title = render 'details/data' data: @details .date
#note
#post_content
.show
h1 = @note.title
= render 'details/data' data: @details
.date
p Created #{time_ago_in_words(@note.created_at)} ago
hr
-if @note.user_id == current_user.id
p=link_to 'Edit', edit_note_path(@note), class: 'btn btn-primary'
p=link_to 'delete', note_path, method: :delete, data: { confirm: "Delete note?" }, class: 'btn btn-primary'
p=link_to 'Add details', new_note_detail_path(@note), class: 'btn btn-primary'
hr
.body
p #{@note.body}
div class="details"
p #{data.body}
=render@note.details
,期望使用名为details/\u detail.html.slim
你可以读到它
details/show.html.slim
用于呈现特定资源的显示操作
details/_detail.html.slim
用于例如为传递details
对象的details/index.html.slim
渲染每一行
如果您的show
模板确实合适,则可以执行以下操作:
# /notes/show.html.slim
= render @note.details.each do |detail|
= render 'details/show', detail: detail
--
=render@note.details
,期望使用名为details/\u detail.html.slim
你可以读到它
details/show.html.slim
用于呈现特定资源的显示操作
details/_detail.html.slim
用于例如为传递details
对象的details/index.html.slim
渲染每一行
如果您的show
模板确实合适,则可以执行以下操作:
# /notes/show.html.slim
= render @note.details.each do |detail|
= render 'details/show', detail: detail
--
这与Rails中集合呈现的工作方式是一致的。
@note.details
是对象的集合,因此在执行渲染@note.details
时,Rails将在集合上循环,对于集合中的每个对象,通过调用该对象上的to_partial_path
来确定要使用哪个部分。在本例中,该方法将返回details/detail
,因此Rails将尝试呈现这种情况。
如果要指定不同的局部,则需要将渲染更改为以下内容:
=render partial:'OTHER_partial',collection:@note。详细信息与Rails中集合呈现的工作方式一致。
@note.details
是对象的集合,因此在执行渲染@note.details
时,Rails将在集合上循环,对于集合中的每个对象,通过调用该对象上的to_partial_path
来确定要使用哪个部分。在本例中,该方法将返回details/detail
,因此Rails将尝试呈现这种情况。
如果要指定不同的局部,则需要将渲染更改为以下内容:
=render partial:'OTHER_partial',collection:@note.details
Rails中的部分名称应以u(_partial.html.slim)开头,然后可以
<%= render :partial => 'tasks/your_partial' %>
”任务/u部分“%”
您可以在Rails中找到的更多信息部分名称应以u(_Partial.html.slim)开头,然后您可以
<%= render :partial => 'tasks/your_partial' %>
”任务/u部分“%”
更多信息您可以找到rails的部分语法是\u name\u of_partial.html.erb/slim/haml
这就是你所犯的错误。
我给您的建议是坚持惯例,如果您希望再次呈现详细信息数据,在另一个视图中,您应该在“详细信息视图”文件夹中有一个部分\u data.html.slim,这样您可以从“详细信息显示”和“笔记显示”中调用它,可能是这样的
notes/show.html.slim
#note
#post_content
.show
h1 = @note.title
= render 'details/data' data: @details
.date
p Created #{time_ago_in_words(@note.created_at)} ago
hr
-if @note.user_id == current_user.id
p=link_to 'Edit', edit_note_path(@note), class: 'btn btn-primary'
p=link_to 'delete', note_path, method: :delete, data: { confirm: "Delete note?" }, class: 'btn btn-primary'
p=link_to 'Add details', new_note_detail_path(@note), class: 'btn btn-primary'
hr
.body
p #{@note.body}
div class="details"
p #{data.body}
details/_data.html.slim
#note
#post_content
.show
h1 = @note.title
= render 'details/data' data: @details
.date
p Created #{time_ago_in_words(@note.created_at)} ago
hr
-if @note.user_id == current_user.id
p=link_to 'Edit', edit_note_path(@note), class: 'btn btn-primary'
p=link_to 'delete', note_path, method: :delete, data: { confirm: "Delete note?" }, class: 'btn btn-primary'
p=link_to 'Add details', new_note_detail_path(@note), class: 'btn btn-primary'
hr
.body
p #{@note.body}
div class="details"
p #{data.body}
我还建议您通读rails的部分语法是\u name\u of_partial.html.erb/slim/haml
这就是你所犯的错误。
我给您的建议是坚持惯例,如果您希望再次呈现详细信息数据,在另一个视图中,您应该在“详细信息视图”文件夹中有一个部分\u data.html.slim,这样您可以从“详细信息显示”和“笔记显示”中调用它,可能是这样的
notes/show.html.slim
#note
#post_content
.show
h1 = @note.title
= render 'details/data' data: @details
.date
p Created #{time_ago_in_words(@note.created_at)} ago
hr
-if @note.user_id == current_user.id
p=link_to 'Edit', edit_note_path(@note), class: 'btn btn-primary'
p=link_to 'delete', note_path, method: :delete, data: { confirm: "Delete note?" }, class: 'btn btn-primary'
p=link_to 'Add details', new_note_detail_path(@note), class: 'btn btn-primary'
hr
.body
p #{@note.body}
div class="details"
p #{data.body}
details/_data.html.slim
#note
#post_content
.show
h1 = @note.title
= render 'details/data' data: @details
.date
p Created #{time_ago_in_words(@note.created_at)} ago
hr
-if @note.user_id == current_user.id
p=link_to 'Edit', edit_note_path(@note), class: 'btn btn-primary'
p=link_to 'delete', note_path, method: :delete, data: { confirm: "Delete note?" }, class: 'btn btn-primary'
p=link_to 'Add details', new_note_detail_path(@note), class: 'btn btn-primary'
hr
.body
p #{@note.body}
div class="details"
p #{data.body}
我还建议您通读以下内容:
分部用前导下划线命名,以区别于常规视图
因此,您需要将details/show.html.slim更改为details/\u show.html.slim
之后,可以在notes/show.html.slim中使用partials
= render partial: "details/show", collection: @note.details, as: :detail
请注意,要渲染集合,使用:collection
选项比each循环短
而details/_show.html.slim就像
# details/_show.html.slim
div.details
p= detail.body
发件人:
分部用前导下划线命名,以区别于常规视图
因此,您需要将details/show.html.slim更改为details/\u show.html.slim
之后,可以在notes/show.html.slim中使用partials
= render partial: "details/show", collection: @note.details, as: :detail
请注意,要渲染集合,使用:collection
选项比each循环短
而details/_show.html.slim就像
# details/_show.html.slim
div.details
p= detail.body