2011-08-21 11 views
5

Jak określić kontroler i działanie w formularzu? Próbuję użyć kontrolera klienta do utworzenia konta i powiązanej osoby ("klienta").Przesyłanie formularza kierowania do innego kontrolera

Oto odpowiednie modele. Osoba należy bezpośrednio do Konta (którego nazywam "Klientem") lub do Lokalizacji i Organizacji na Koncie.

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 

A oto formularz, aby utworzyć „klient” Staram się, aby wraz z resztą kodu:

<%= 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 

A oto moje trasy:

ShopManager::Application.routes.draw do 

resources :accounts 
resources :organizations 
resources :locations 
resources :people 
resources :addresses 

get 'clients/new' 
post 'clients' 

end 

Podczas próby renderowania formularza pojawia się następujący błąd:

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 /> 

Odpowiedz

12

Trzeba powiedzieć to w routes.rb

resources :clients 

W formularzu należy podać adres URL jako clients_path z metody jako post:

<%= form_for @account, :url => clients_path, :html => {:method => :post} do |f| %> 
--- 
<% end 

Aby uzyskać więcej informacji, jak szyny obsługuje adresy URL REST: http://microformats.org/wiki/rest/urls

+0

Czy możesz opublikować wiersz numer 9? –

+1

To działa idealnie! Dzięki za pomoc! Brakowało accepts_nested_attributes_for: osób w moim modelu konta, kiedy opublikowałem swój ostatni komentarz. Przepraszam za to. –

+0

Serdecznie zapraszamy :-) –

Powiązane problemy