0

RoR初心者のビット。モデル属性をフォームに表示し、ユーザーがその値を変更してフォームを「送信」できるようにしたいと考えています。属性が属するクラスの「ベース」コントローラではないコントローラにフォームをルーティングし、「セカンダリ」コントローラに DB の属性を更新させたいと考えています。フォームのコードは次のとおりです。

<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 で更新されます。

ユーザーが [Update Out 1] または [Update Out 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」コントローラーにルーティングするにはどうすればよいですか?

ユーザーが入力した値を「Update Out」ボタンに取り込むにはどうすればよいですか?

「button_to コード」の場所によって、button_to オプションで指定されたルーティングが変わるのはなぜですか?

複数のモデルのデータを表示するフォームから 1 つの属性だけを更新したいと考えています。正しいRails Wayとは?

4

2 に答える 2

0

ビューに複数のモデルがあり、特定のモデルを更新したい場合は、その特定のモデルを渡すか、params ハッシュの url を介して単に id および変更された属性にすることができます。しかし、それは主にあなたが達成しようとしているものに依存します。明確にするために、正確な問題を書き直す必要があるかもしれません。

于 2013-08-02T14:26:49.493 に答える