【问题标题】:Rails routing error: No route matches [GET] "/conversations/'id'/reply"Rails 路由错误:没有路由匹配 [GET] "/conversations/'id'/reply"
【发布时间】:2014-06-08 19:21:12
【问题描述】:

我在回复与 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: [: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

相关的rake路线:

                    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.

【问题讨论】:

    标签: ruby-on-rails routing mailboxer


    【解决方案1】:

    路由是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| %>
    

    【讨论】:

    • 谢谢。尽管我的问题是首先尝试通过单击对话索引视图中的 link_to 来呈现此表单。但我能够用你给我的信息来修复它。至少现在。谢谢,非常感谢。
    【解决方案2】:

    您的 rake 路由显示 reply_conversation POST /conversations/:id/reply(.:format) conversations#reply,但您的 link_to 将发送 get 请求。您需要更改路线以使其成为获取请求。

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多