簡體   English   中英

在Rails 4中回復與Mailboxer gem的對話

[英]Replying to conversations with Mailboxer gem in Rails 4

我在回復與Rails 4中的Mailboxer gem的對話時遇到麻煩。關於此gem的文檔不多,或者我只是經驗不足,但是我在此問題上停留了一段時間現在。

查看/對話/索引:( 顯示當前所有用戶對話的列表)

<% @conversations.each do |conversation| %>
<%= link_to conversation.subject, reply_conversation_path(conversation.id) %>
<%= conversation.updated_at.strftime("%a, %m/%e/%Y %I:%M %p") %>
<%= link_to "Move to Trash", {:controller => "conversations", :action => 
    "trash", :id => conversation.id}, :title=> "Move to Trash", :method=>'post' %>
<% end %>

單擊上面視圖中的第一個link_to時,收到路由錯誤: No route matches [GET] "/conversations/68/reply"

我希望它呈現以下視圖,並傳遞正確的對話信息:

查看/消息/ _form / (用於回復現有對話)

Reply:
<%= form_for :message, url: :conversation do |f| %>
<%= f.text_area :body %>
<%= f.submit "Send Message", class: 'btn btn-primary' %>
<%= submit_tag 'Clear Reply Box', type: :reset, class: 'btn btn-danger' %>
<% end %>

路線:

resources :users
root to: 'profiles#index'

resources :messages do
  member do
      post :new
  end
end
resources :conversations do
  member do
      post :reply
      post :trash
      post :untrash
  end
  collection do
      get :trashbin
      post :empty_trash
  end
end

會話控制器:

class ConversationsController < ApplicationController
before_filter :authenticate_user!
helper_method :mailbox, :conversation
def index
    @conversations ||= current_user.mailbox.inbox.all
end

def reply
    current_user.reply_to_conversation(conversation, *message_params(:body, :subject))
    redirect_to conversation
end

def trashbin
    @trash ||= current_user.mailbox.trash.all
end

def trash
    conversation.move_to_trash(current_user)
redirect_to :conversations
end

def untrash
    conversation.untrash(current_user)
redirect_to :back
end

def empty_trash
    current_user.mailbox.trash.each do |conversation|
        conversation.receipts_for(current_user).update_all(:deleted => true)
    end
redirect_to :conversations
end

private

def mailbox
    @mailbox ||= current_user.mailbox
end

def conversation

    @conversation ||= mailbox.conversations.find(params[:id])
end

def conversation_params(*keys)
    fetch_params(:conversation, *keys)

end

def message_params(*keys)
    fetch_params(:message, *keys)
end

def fetch_params(key, *subkeys)
    params[key].instance_eval do
        case subkeys.size
            when 0 then self
            when 1 then self[subkeys.first]
            else subkeys.map{|k| self[k] }
        end
    end
end
end

郵件控制器:

class MessagesController < ApplicationController

# GET /message/new
def new

    @request = Request.find(params[:request])
    @message = current_user.messages.new
    @user = @request.user
end

# POST /message/create
def create

@user = User.find(params[:user])
@body = params[:body]
@subject = params[:subject]

current_user.send_message(@user, params[:body], params[:subject])
flash[:notice] = "Message has been sent!"
redirect_to :conversations
end
end

相關耙路:

                    users GET    /users(.:format)                     users#index
                      POST   /users(.:format)                     users#create
             new_user GET    /users/new(.:format)                 users#new
            edit_user GET    /users/:id/edit(.:format)            users#edit
                 user GET    /users/:id(.:format)                 users#show
                      PATCH  /users/:id(.:format)                 users#update
                      PUT    /users/:id(.:format)                 users#update
                      DELETE /users/:id(.:format)                 users#destroy
                 root GET    /                                    profiles#index
              message POST   /messages/:id(.:format)              messages#new
             messages GET    /messages(.:format)                  messages#index
                      POST   /messages(.:format)                  messages#create
          new_message GET    /messages/new(.:format)              messages#new
         edit_message GET    /messages/:id/edit(.:format)         messages#edit
                      GET    /messages/:id(.:format)              messages#show
                      PATCH  /messages/:id(.:format)              messages#update
                      PUT    /messages/:id(.:format)              messages#update
                      DELETE /messages/:id(.:format)              messages#destroy
   reply_conversation POST   /conversations/:id/reply(.:format)   conversations#reply
   trash_conversation POST   /conversations/:id/trash(.:format)   conversations#trash
 untrash_conversation POST   /conversations/:id/untrash(.:format) conversations#untrash
trashbin_conversations GET    /conversations/trashbin(.:format)    conversations#trashbin
empty_trash_conversations POST   /conversations/empty_trash(.:format) conversations#empty_trash
        conversations GET    /conversations(.:format)             conversations#index
                      POST   /conversations(.:format)             conversations#create
     new_conversation GET    /conversations/new(.:format)         conversations#new
    edit_conversation GET    /conversations/:id/edit(.:format)    conversations#edit
         conversation GET    /conversations/:id(.:format)         conversations#show
                      PATCH  /conversations/:id(.:format)         conversations#update
                      PUT    /conversations/:id(.:format)         conversations#update
                      DELETE /conversations/:id(.:format)         conversations#destroy

為了讓您了解到目前為止的情況,我一直在關注本教程: http : //jamestansley.com/2014/02/22/customizing-the-mailboxer-ruby-gem-2/

我猜想我需要更多地研究路由,但是我需要在周末結束之前完成此過程,非常感謝您的幫助。

萬一我遺漏了任何東西,這是我的github存儲庫: https : //github.com/portOdin/GoFavorIt-Heroku/tree/stackflow/app

更新:

我已經解決了這個問題,如下所述,盡管現在我還有另一個問題。

'nil:NilClass的未定義方法'[]'

在此處輸入圖片說明

這個問題越來越大,所以除非您能在這里幫助我,否則我可能稍后再打開另一個。

我替換了該行:

 <%= link_to conversation.subject, reply_conversation_path(conversation.id) %> 

與:

<%= link_to "Reply To Conversation", {:controller => "conversations", :action => 
"reply", :id => conversation.id}, :title=> "Reply To Conversation", :method=>'post' %>

我剛剛復制並修改了“移至垃圾箱”鏈接。 不確定這是否是最好的方法,但是現在工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM