如何在表单提交中指定控制器和操作?我正在尝试使用客户"控制器来创建帐户和关联人员(客户").
How do I specify the Controller and Action in a form submission? I am trying to use a 'Clients' Controller to create an Account and an associated Person ('Client').
以下是相关模型.一个人要么直接属于一个帐户(我称之为客户"),要么属于一个帐户内的位置和组织.
Here are the pertinent models. A Person belongs either to an Account directly (which I am calling a 'Client') or to a Location and Organization within an Account.
class Account < ActiveRecord::Base has_many :organizations has_many :persons, :as => :linkable accepts_nested_attributes_for :organizations end class Person < ActiveRecord::Base belongs_to :linkable, :polymorphic => true end这是我正在尝试与其余代码一起创建的用于创建客户端"的表单:
And here is the form to create a 'Client' I am trying to make along with the rest of the code:
<%= form_for @account, :url => { :controller => "clients_controller", :action => "create" } do |f| %> <%= f.fields_for :persons do |builder| %> <%= builder.label :first_name %><br /> <%= builder.text_field :first_name %><br /> <%= builder.label :last_name %><br /> <%= builder.text_field :last_name %><br /> <%= builder.label :email1 %><br /> <%= builder.text_field :email1 %><br /> <%= builder.label :home_phone %><br /> <%= builder.text_field :home_phone %><br /> <% end %> <%= f.submit "Add client" %> <% end %> class ClientsController < ApplicationController def new @account = Account.new @person = @account.persons.build end def create @account = Account.new(params[:account]) if @account.save flash[:success] = "Client added successfully" render 'new' else render 'new' end end end这是我的路线:
ShopManager::Application.routes.draw do resources :accounts resources :organizations resources :locations resources :people resources :addresses get 'clients/new' post 'clients' end尝试渲染表单时,出现以下错误:
When trying to render the form, I get the following error:
ActionController::RoutingError in Clients#new Showing C:/Documents and Settings/Corey Quillen/My Documents/rails_projects/shop_manager/app/views/clients/new.html.erb where line #1 raised: No route matches {:controller=>"clients_controller", :action=>"create"} Extracted source (around line #1): 1: <%= form_for @account, :url => { :controller => "clients_controller", :action => "create" } do |f| %> 2: 3: <%= f.fields_for :persons do |builder| %> 4: <%= builder.label :first_name %><br /> 推荐答案这个你得在routes.rb里说
You have to say this in routes.rb
resources :clients在表单中,指定url为clients_path,方法为post:
In the form, specify the url as clients_path with method as post:
<%= form_for @account, :url => clients_path, :html => {:method => :post} do |f| %> --- <% end有关 rails 如何处理 REST url 的更多信息:microformats/wiki/rest/urls
For more information how rails handles REST urls: microformats/wiki/rest/urls
更多推荐
路由表单提交到不同的控制器
发布评论