2014-06-08 176 views
0

我無法回覆與Rails 4中的Mailboxer gem的對話。關於此問題沒有太多文檔寶石,也許我只是沒有足夠的經驗,但我一直堅持這一段時間了。Rails路由錯誤:沒有路由匹配[GET]「/ conversations /'id'/ reply」

視圖/會話/索引: (所有當前用戶的對話的節目的列表)

<% @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: [:reply, 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 

相關耙路線:

我一直在關注這個教程:http://jamestansley.com/2014/02/22/customizing-the-mailboxer-ruby-gem-2/

如果我遺漏了任何東西,這裏是我的github回購: https://github.com/portOdin/GoFavorIt-Heroku/tree/stackflow/app

回答

2

路線是post路線:

resources :conversations do 
    member do 
     post :reply 

你的形式需要使用HTTP POST請求,因爲你沒有指定一個方法,它默認爲GET請求。

替換此...

<%= form_for :message, url: [:reply, conversation] do |f| %> 

與此:

<%= form_for :message, url: [:reply, conversation], method: :post do |f| %> 
+0

Thankyou。雖然我的問題是試圖通過點擊會話索引視圖中的link_to來首先獲取此表單。但是我可以用你給我的信息修復它。至少現在。謝謝,我真的很感激。 – domi91c

0

你耙路線顯示reply_conversation POST /conversations/:id/reply(.:format) conversations#reply但你的link_to將發送get請求。您需要更改路線以使其成爲獲取請求。

相關問題