2015-12-04 87 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