路由表单提交到不同的控制器

编程入门 行业动态 更新时间:2024-10-27 14:23:34
本文介绍了路由表单提交到不同的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

路由表单提交到不同的控制器

本文发布于:2023-10-26 10:54:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   路由   控制器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!