2016-02-04 16 views
1

Rails新手在這裏。我正在嘗試這個郵箱寶石,並在我的網站上實施。我想創建一個收件箱系統,但不知何故它沒有向我顯示對話顯示頁面。它給我以下錯誤:Rails - NameError undefined局部變量或方法`對話'

NameError in Conversations#show Showing C:/Sites/myinbox/app/views/conversations/show.html.haml where line #6 raised:

undefined local variable or method `conversation'

我不會在哪裏出錯。我正在關注如何設置它的教程here,但我沒有收到要顯示的對話。

我真的很感激,如果你願意幫忙!

show.html.haml

.row 
    .spacer 
    .col-md-6 
    = link_to "Compose", new_conversation_path, class: "btn btn-success" 
    .col-md-6.text-right 
    - if conversation.is_trashed?(current_user) <!-- LINE 6 --> 
     = link_to 'Untrash', untrash_conversation_path(conversation), class: 'btn btn-info', method: :post 
    - else 
     = link_to 'Move to trash', trash_conversation_path(conversation), class: 'btn btn-danger', method: :post, | 
     data: {confirm: 'Are you sure?'}                  | 
.row 
    .spacer 
    .col-md-4 
    .panel.panel-default 
     .panel-body 
     = render 'mailbox/folders' 
    .col-md-8 
    .panel.panel-default 
     .panel-body 
     = render partial: 'messages' 
     .panel-footer 
     /Reply Form 
     = form_for :message, url: reply_conversation_path(conversation) do |f| 
      .form-group 
      = f.text_area :body, placeholder: "Reply Message", rows: 4, class: "form-control" 
      = f.submit "Reply", class: 'btn btn-danger pull-right' 

conversations_controller.rb

class ConversationsController < ApplicationController 
    before_action :authenticate_user! 

    def new 
    end 

    def create 
    recipients = User.where(id: conversation_params[:recipients]) 
    conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation 
    flash[:notice] = "Your message was successfully sent!" 
    redirect_to conversation_path(conversation) 
    end 

    def show 
    @receipts = conversation.receipts_for(current_user).order("created_at ASC") 
    # mark conversation as read 
    conversation.mark_as_read(current_user) 
    end 


    def reply 
    current_user.reply_to_conversation(conversation, message_params[:body]) 
    flash[:notice] = "Your reply message was successfully sent!" 
    redirect_to conversation_path(conversation) 
    end 

    def trash 
    conversation.move_to_trash(current_user) 
    redirect_to mailbox_inbox_path 
    end 

    def untrash 
    conversation.untrash(current_user) 
    redirect_to mailbox_inbox_path 
    end 


    private 

    def conversation_params 
    params.require(:conversation).permit(:subject, :body,recipients:[]) 
    end 

    def message_params 
    params.require(:message).permit(:body, :subject) 
    end 
end 
+0

您需要將'conversation'聲明爲'@ conversation'。因爲視圖只能訪問實例變量。 –

+0

本教程的下一步(在此控制器中創建'對話'輔助方法的那一步)應該有所幫助。 –

回答

0

的show.html.haml文件不能直接訪問的談話,因此,你需要創建一個實例變量,即@Conversation = Conversations_controller.rb中顯示的對話。

0

多一些現有答案的推理。

打破下來了片刻:if conversation.is_trashed?(current_user)

if calling the method is_trashed with parameter current_user on the variable conversation results in a truthy value then continue

哪裏變conversation從何而來?如果你認爲你的觀點是一種方法,你會注意到一些東西 - 它沒有在任何地方定義conversation

爲了讓您的視圖看到你的控制器內變量,你必須使它的實例變量。在紅寶石中,這是通過在其前面加上@來完成的(是的,它有點語法魔術 - 它們的封閉方法可以看到「常規」變量,但名稱以@開頭的變量對整個實例是可見的)。

相關問題