Rails 4 使用子域和自定义路由更新模型

Rails 4 update model with subdomain and custom route

我无法使用我的自定义路由(没有资源)和一个子域更新一个用户模型。

我的表格是:

<%= form_for :user, :url => services_users_update_url(@user), :html => { :method => 'put', :multipart => true } do |f| %>

而我的子域是 "services"。

我的路线文件:

constraints :subdomain => "services" do  
  scope :module => "services", :as => "services" do
    match '/users/update/' => 'users#update', via: [:put]
  end
end

当我显示我的表单页面时,我的表单中生成的 url 是:

http://services.website.dev:3000/8296/users/update

当我确认我的表格时,错误信息是:

No route matches [PUT] "/8296/users/update"

我的路线有什么问题?

如果我尝试:

match '/users/:id' => 'users#update', via: [:put]

表单也会生成路由错误。

我也试过:

match '/users/update/:id' => 'users#update', via: [:put]

没有成功。

编辑:我想添加更多信息。

我的表单页面位于子域 'cloud' 中,我的更新操作位于子域 'services'.

根据您的url:

http://services.website.dev:3000/8296/users/update
                                ^^^^/^^^^/^^^^^
                                :id/users/update

你的路线应该是:

constraints :subdomain => "services" do  
  scope :module => "services", :as => "services" do
    put ':id/users/update' => 'users#update', as: :services_users_update
  end
end

阅读documentation

您可以使用内置的 resources 方法来生成您的路线:

constraints subdomain: "services" do  
  scope module: "services", as: "services" do
    resources :users, only: [:update]
  end
end