2015-12-04 82 views
0

你好,我想表现出/notes/show_html.slim @detail,但我有错误缺少的部分细节/ _detail与在/notes/show.html.slim缺少导轨部分

我怎么能输出@detail

routes.rb中

Rails.application.routes.draw do 
    root 'static_pages#home' 
    match '/about_me', to: 'static_pages#about', via: 'get' 
    devise_for :users 
    resources :users, only: [:index, :show] 
    resources :notes do 
    resources :details 
    end 
end 

details_controller.rb

class DetailsController < ApplicationController 
    def new 
    @note = Note.find(params[:note_id]) 
    @detail = Detail.new 
    end 

    def create 
    @note = Note.find(params[:note_id]) 
    @detail = @note.details.build(detail_param) 
    if @detail.save 
    redirect_to note_path(@note) 
    end 
end 

    def destroy 
    @note = Note.find(params[:note_id]) 
    @detail = @note.details.find(params[:id]) 
    @detail.destroy 
    redirect_to note_path(@note) 
    end 

    private 

    def detail_param 
    params.require(:detail).permit(:body) 
    end 
end 

和视图

/notes/show.html.slim

#note 
    #post_content 
    .show 
    h1 = @note.title 
    = render @note.details #or = render 'details/show'? 
    .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} 

/details/show.html.slim

div class="details" 
    p #{@detail.body} 

回答

3

= render @note.details,预计将呈现使用称为details/_detail.html.slim

模板的每个对象(detail)你可以阅读一下here

details/show.html.slim是为了使特定资源的表演动作。

details/_detail.html.slim是指例如,渲染每行details/index.html.slim传递detail对象。

如果您show模板真正适合,那么你可以做这样的事情:

# /notes/show.html.slim 
= render @note.details.each do |detail| 
    = render 'details/show', detail: detail 

-

# /details/show.html.slim 
- detail ||= @detail 
div class="details" 
    p #{detail.body} 
+0

**谢谢!**工作! –

0

这是如何收集渲染Rails的作品是一致的。 @note.details是对象的集合,因此当您执行render @note.details时,Rails将遍历集合,并且集合中的每个对象都通过调用该对象上的to_partial_path来确定要使用哪个部分。在这种情况下,该方法将返回details/detail,这就是Rails将尝试呈现的内容。 如果要指定一个不同的部分,你需要渲染更改为类似:

= render partial: 'OTHER_PARTIAL', collection: @note.details

0
Rails中

部分名称应与_(_partial.html.slim)开始,接下来,您可以

<%= render :partial => 'tasks/your_partial' %> 

更多信息,你可以找到here

0

钢轨的部分语法是_name_of_partial.html.erb /超薄/ HAML 这就是你得到的错误。 我对你的建议是坚持约定,如果你想再次呈现细节数据,在另一个视图中,你应该在你的details视图文件夹中有一个部分,所以你可以调用它从在细节上和从展会上你的笔记显示展会上,它可以像这样

笔记/ 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} 

细节/ _data.html.slim

div class="details" 
    p #{data.body} 

我也建议你阅读throughly http://guides.rubyonrails.org/layouts_and_rendering.html

0

Rails doc

片段命名时使用一个前导下划线从常规视图

所以区分你需要更改details/show.html.slim详情/ _show.html.slim

之后,你可以用谐音在笔记/ show.html.slim像

= render partial: "details/show", collection: @note.details, as: :detail 

注意,呈现的集合,使用:collection选择是不是每个回路短。

和细节/ _show.html.slim就像

# details/_show.html.slim 
div.details 
    p= detail.body