路由表单提交到不同的控制器 [英] Routing form submission to a different controller

查看:35
本文介绍了路由表单提交到不同的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在表单提交中指定控制器和操作?我正在尝试使用客户"控制器来创建帐户和关联人员(客户").

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 的更多信息:http://microformats.org/wiki/rest/urls

For more information how rails handles REST urls: http://microformats.org/wiki/rest/urls

这篇关于路由表单提交到不同的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆