form_for尝试使用子域访问不存在的path_helper

时间:2012-03-08 06:14:03

标签: ruby-on-rails ruby-on-rails-3 controller routes form-for

在我的“购买”子域中,我有以下路线:

  constraints Subdomain::Buy do
    scope module: :buy do
      resources :users do
        resources :messages
      end

我正在使用以下代码制作新的消息表单:

<%= form_for [@user,@message] do |f| %>

控制器操作:

class Buy::MessagesController < ApplicationController
  def new
    @user = User.find(params[:user_id])
    @message = Buy::Message.new(receiver: @user, sender: current_user)
    render
  end

然而,该页面抛出以下异常:

undefined method `user_buy_messages_path'

当我查看rake routes时,我会得到以下方法:

          user_messages GET    /users/:user_id/messages(.:format)                           buy/messages#index
                           POST   /users/:user_id/messages(.:format)                           buy/messages#create
          new_user_message GET    /users/:user_id/messages/new(.:format)                       buy/messages#new
         edit_user_message GET    /users/:user_id/messages/:id/edit(.:format)                  buy/messages#edit
              user_message GET    /users/:user_id/messages/:id(.:format)                       buy/messages#show
                           PUT    /users/:user_id/messages/:id(.:format)                       buy/messages#update
                           DELETE /users/:user_id/messages/:id(.:format)                       buy/messages#destroy

所以问题是,我不确定为什么子域'买'会悄悄进入。form_for应该使用user_messages_path而不是user_buy_messages_path


P.S:

型号:

class Buy::Message < ActiveRecord::Base
  belongs_to :sender, class_name: '::User'
  belongs_to :receiver, class_name: '::User'
end

1 个答案:

答案 0 :(得分:1)

您可以通过以下方式强制执行form_for的网址:

<%= form_for [@user,@message], :url => user_messages_path do |f| %>