如何将资源路由到其控制器?我在编辑页面中使用不同模型的资源,因此我的操作首先被路由到其模型控制器。
此编辑页面来自
class Grandstreamers::ResellersController < ApplicationController
def new
end etc...
我正在尝试将请求路由到:
Grandstreamers::CertificatesController < ApplicationController
def new
end
def update
end etc...
这是我在views / grandstreamers / resellers / edit.html.erb
下的表单<%= form_for @untrained, :url => certificates_update_path(@untrained) do |f| %>
<p> Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(@current_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Un-Train", class: "btn btn-large btn-primary" %>
<% end %>
<%= form_for @trained, :url => certificates_create_path(@trained) do |f| %>
<p> Non-Trained Users </p>
<%= select_tag "certificate[user_id]", options_for_select(@non_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
<%= f.submit "Train", class: "btn btn-large btn-primary" %>
<% end %>
我的路线是:
resources :certificates
请注意
:url => certificates_create_path
不正确且无效。有没有办法在routes.rb文件或我的表单中指定此路由?谢谢
修改
这是我的resellers_controller edit(),它是第一个路由。
@trained = Certificate.new(params[:certificate])
#Trying to get to certificates_controller update. Then update the :attend to "No"
#@untrained = Certificate.new(params[:certificate])
#@untrained = Certificate.find(params[:id])
#@untrained = Certificate.find_by_user_id(params[:id])
@untrained没有定义,我不知道如何让它去我的证书控制器。对于@trained,我可以定义它,因为它尚未制作,并且当它找不到正确的值时不会给我错误。
我的证书控制器使用create()但无法获取更新()
def create
@trained = Certificate.new(params[:certificate])
if @trained.save
@trained.update_attributes(attend: "Yes")
end
redirect_to grandstreamers_resellers_path
end
def update
@untrained = Certificate.find(params[:id])
@untrained.update_attributes(attend: "No")
redirect_to grandstreamers_resellers_path
end
主要问题 需要在reseller_controller中以某种方式定义实例变量@trained和@untrained。我可以将它们定义为加载编辑页面吗?
部分解决方案
我在我的resellers_controller中定义了它,它现在加载了编辑页面。
@untrained = User.find(params[:id])
现在我收到了这个错误:
No route matches [PUT] "/certificates.1"
答案 0 :(得分:1)
我认为问题是你需要让路由知道控制器的全名。
scope module: 'Grandstreamers' do
resources :certificates
end
创建表单时使用以下路径:
<%= form_for @untrained, :url => certificate_path(@untrained), :method => :put do |f| %>
<%= form_for @trained, :url => certificates_path, :method => :post do |f| %>
答案 1 :(得分:0)
使用他的routes.rb文件
namespace :grandstreamers do
resources :certificates
end