leo*_*nel 3 ruby forms ruby-on-rails
我有这张表,您可以在其中添加税金.
|Name | Rate | Description |
|IVA | 16 | something |
| | | |
| | | |
| | | |
save
Run Code Online (Sandbox Code Playgroud)
因此,如果您点击保存,它将保存输入的新项目.我必须在tax_controller.rb中这样做
@account = Account.find(current_user.account_id)
3.times {@account.taxes.build}
Run Code Online (Sandbox Code Playgroud)
然后在表格中
<%= 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 %>
...
Run Code Online (Sandbox Code Playgroud)
当我提交表单时,字段会保存在数据库中.问题是它重定向到帐户显示页面; 我明白它必须这样做因为form_for(@account).
所以问题是:如何在提交后指定我希望表单重定向的位置.在这种情况下,我想将其重定向到当前页面.
这只是间接的原因form_for(@account).
当您发布表单时,它会触及accounts_controller的创建(或更新)操作.
因此,您应该执行此控制器的这两个操作(创建和更新)redirect_to ....
你说你想重定向到当前页面.当前页面究竟是什么?
好的,你可以做的是将它添加到你的路线:
resources :accounts, :module => "taxes"
Run Code Online (Sandbox Code Playgroud)
你的形式就会变成
form_for [:taxes, @account] ... do |f|
Run Code Online (Sandbox Code Playgroud)
你的控制器会在 app/controllers/taxes/accounts_controller.rb
class Taxes::AccountsController < ::AccountsController
def edit
end
def update
...
redirect_to taxes_url
end
end
Run Code Online (Sandbox Code Playgroud)
所以你必须用这种方法改变你的形式.您可以将路径([@account]或[:taxes,@ account])作为您的部分参数...
另一个解决方案,可能更简单,就是在表单中输入redirect_to.只有在税收中使用表单时才设置它.在控制器中,unless params[:redirect_to].blank?; redirect_to params[:redirect_to] end......
| 归档时间: |
|
| 查看次数: |
6421 次 |
| 最近记录: |