将表单提交路由到不同的控制器
如何在表单提交中指定Controller和Action? 我正在尝试使用“客户端”控制器来创建帐户和关联人员(“客户”)。
以下是相关模型。 一个人直接属于账户(我称之为“客户”)或账户中的位置和组织。
class Account :linkable accepts_nested_attributes_for :organizations end class Person true end
以下是创建“客户端”的表单,我试图与其余代码一起制作:
{ :controller => "clients_controller", :action => "create" } do |f| %>
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
尝试呈现表单时,我收到以下错误:
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: { :controller => "clients_controller", :action => "create" } do |f| %> 2: 3: 4:
你必须在routes.rb中这样说
resources :clients
在表单中,将url指定为clients_path,方法为post:
<%= form_for @account, :url => clients_path, :html => {:method => :post} do |f| %> --- <% end
有关rails如何处理REST URL的更多信息: http : //microformats.org/wiki/rest/urls
- Rails 4.1 Mailer预览和设计自定义电子邮件
- Rails应用程序错误 – ActiveRecord :: PendingMigrationError正在等待迁移; 运行’rake db:migrate RAILS_ENV = development’来解决此问题
- 如何从Rails中的MySQL存储过程中获取输出参数?
- 为什么“范围”导向的行为(特别是“索引”行动)在Pundit中有不同的处理方式?
- 让Rails测试意识到Rails内部链外的Rack中间件
- 在rails上的ruby中运行测试时有一个默认端口
- 已安装的Rails但rails命令表明它未安装
- rails中的一对一或零关联
- rails g迁移错误