2012-01-04 27 views
3

我有这张表添加税款。Rails 3.更改表单提交时的重定向

|Name | Rate | Description | 
|IVA | 16 | something | 
|  |  |    | 
|  |  |    | 
|  |  |    | 
save 

所以,如果你点击保存,将保存输入的新项目。我有当我提交表单为此在taxes_controller.rb

@account = Account.find(current_user.account_id) 
3.times {@account.taxes.build} 

那么这个形式

<%= form_for(@account) do |f| %> 
<table style="width:400px;" class="taxes"> 
    <tr> 
    <th>Name</th> 
    <th>Rate</th> 
    <th>Description</th> 
    </tr> 

<%= f.fields_for :taxes do |builder| %> 
    <tr> 
    <td><%= builder.text_field :name %></td> 
    <td><%= builder.text_field :rate %> %</td> 
    <td><%= builder.text_field :identification %></td> 
    </tr> 
<% end %> 
... 

,该领域也得到保存在数据库中。问题是它重定向到账户显示页面;我知道它必须这样做,因为form_for(@account)

所以问题是:如何指定提交后表单要重定向的位置。在这种情况下,我想将它重定向到currect页面。

回答

6

这只是间接的,因为form_for(@account)

当您发布表单时,它会触发accounts_controller的创建(或更新)操作。

因此,在这2个动作(创建和更新)这个控制器,你应该做一个redirect_to ...

你说你想重定向到当前页面。什么是当前页面?


好了,你可以做的是添加到您的路线:

resources :accounts, :module => "taxes" 

和你的形式将成为

form_for [:taxes, @account] ... do |f| 

你的控制器将是app/controllers/taxes/accounts_controller.rb

class Taxes::AccountsController < ::AccountsController 
    def edit 

    end 

    def update 
     ... 
     redirect_to taxes_url 
    end 
end 

所以你用这种方法必须改变你的形式。您可以将路径([@account]或[:taxes,@account])作为您的部分参数传递给您...

另一种解决方案,也许更简单,只需在您的表单中添加redirect_to输入即可。您只有在您使用表格缴纳税款时才设置。并在控制器中,unless params[:redirect_to].blank?; redirect_to params[:redirect_to] end ...

+0

是的,但我也使用帐户控制器来创建新的帐户。但在上面的表格中,我只用它来创建新的税收。当前页面是http:// localhost:3000/taxes – leonel 2012-01-04 21:28:59

+0

我更新了我的信息 – Robin 2012-01-04 21:47:14

+0

我认为在accounts_controller中你的意思是class Taxes :: AccountsController ' – leonel 2012-01-04 22:13:42