gpt4 book ai didi

ruby-on-rails - 路由表单提交到不同的 Controller

转载 作者:行者123 更新时间:2023-12-04 17:03:55 25 4
gpt4 key购买 nike

如何在表单提交中指定 Controller 和操作?我正在尝试使用“客户” Controller 来创建帐户和关联人员(“客户”)。

以下是相关模型。一个人直接属于一个帐户(我称之为“客户”)或属于一个帐户内的位置和组织。

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

这是我正在尝试与其余代码一起创建的“客户端”的表单:
<%= 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

尝试呈现表单时,出现以下错误:
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中说这个

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

关于ruby-on-rails - 路由表单提交到不同的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7136126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com