【发布时间】:2013-08-02 13:54:14
【问题描述】:
有点 RoR 新手。我想在表单上显示模型属性,为用户提供更改其值和“提交”表单的能力。我想将表单路由到不是属性所属类的“基本”控制器的控制器,并让“辅助”控制器更新数据库中的属性。这是表单的代码:
<h1>Edit Number of Current Calls</h1>
<%= form_for(@client) do |f| %>
<% if @client.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@client.errors.count, "error") %>
prohibited this client from being saved:</h2>
<ul>
<% @client.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Number of Current Calls:" %><br />
<%= f.text_field :current_call_count %>
</div>
<%= button_to 'Update In', { :controller => "client_profiles", :action => "update" }, :method => :put %>
<% end %>
<%= button_to 'Update Out 1', { :controller => "client_profiles", :action => "update" }, :method => :put %>
<%= button_to 'Update Out 2', { :controller => "client_profiles", :action => "update", :client => {:current_call_count => @client.current_call_count} }, :method => :put %>
如果用户“点击”“Update In”按钮,他们的提交将被路由到“clients”控制器,并在 DB 中更新属性。
如果用户单击“更新输出 1”或“更新输出 2”按钮,他们的提交将被路由到“client_profiles”控制器,但属性不会更新。这是“client_profiles”控制器中的相关代码:
def update
@client = Client.find(params[:id])
@client.update_attributes(params[:client])
redirect_to show_phone_client_profile_path(@client)
end
如何将“Update In”提交路由到“client_profiles”控制器?
如何捕获用户在“更新输出”按钮中输入的值?
为什么“button_to 代码”的位置会改变 button_to 选项中指向的路由?
我真的想只更新显示多个模型数据的表单中的一个属性。什么是正确的 Rails 方式?
【问题讨论】:
-
这里发生了很多事情,还有一些事情需要改变。也许您可以用 1 或 2 句话描述您想要的功能?
标签: ruby-on-rails