2013-05-10 28 views
-1

我認爲這非常簡單,但無法找到任何關於如何操作的解釋。在一個視圖中創建一個form_for,發佈到另一個視圖的控制器

我有一個形式的配置文件頁面,當提交時應該通過CompletedSetsController,但我不知道如何告訴form_for我想通過已編寫的CompletedSetsController創建/更新,而不是重寫CRUD for CompletedSets ProfilesController

My CompletedSetsController是腳手架生成的CRUD控制器。

這裏是我的形式:

現在,如果我在ProfileController@completed_set = CompletedSet.new這種形式只適用,但是這不是很乾,尤其是如果我要改寫在ProfilesController的創建和更新方法。

<%= form_for(@completed_set) do |f| %> 
     <tr> 
      <td><%= we.exercise.name %> 
       <%= f.hidden_field :exercise_id, :value => we.exercise.id %> 
       <%= f.hidden_field :user_id, :value => current_user.id %> 
       <%= f.hidden_field :program_id, :value => @user_program.id %> 
       <%= f.hidden_field :group_id, :value => @user_group.id %> 
       <%= f.hidden_field :workout_id, :value => @workout.id %> 
      </td> 
      <td><%= we.set %></td> 
      <td><%= f.number_field :repetitions, :value => we.repetitions %></td> 
      <td><%= if we.exercise.try(:is_cardio) == false then f.text_field :weight end %></td> <!-- :value => value of previous time user did this exercise --> 
      <td><%= if we.rest.to_i >= 60 then pluralize(we.rest.to_f/60, 'minute') else pluralize(we.rest.to_i, 'second') end %></td> 
      <td><%= if we.exercise.try(:is_cardio) == true then pluralize(we.time, 'minute') end %></td> 
      <!-- <td><%#= if we.exercise.try(:is_cardio) == true then f.number_field :rpe, :value => we.rpe end %></td> Forgot to put this in table --> 
      <td><%= link_to 'Remove', workout_workout_exercise_path(@workout, we), method: :delete %></td> 
      <td><%= f.submit %></td> 
     </tr> 
     <% end %> 

編輯:

更具體一點,我的個人資料頁是一個semi-static頁面在UsersController我UsersController很簡單:

class UsersController < ApplicationController 

    def profile 
     # @completed_set = CompletedSet.new 
    end 

    def settings 
    end 

end 

我所有的控制器邏輯是在CompletedSetsController我試圖避免複製到UsersController

+0

*在Google上的* ruby​​ rails形式,第一個結果:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for ... – Raindal 2013-05-10 14:27:19

+0

感謝您分享您的谷歌搜索結果與我,但這並不能回答我的問題。 – Arel 2013-05-10 14:34:27

+0

在一個視圖中創建一個form_for **,併發布到另一個視圖的控制器**,我的「google結果」告訴您<%= form_for @post,:as =>:post,:url => posts_path ... '如果只是花時間向下滾動並閱讀並看到url屬性 – Raindal 2013-05-10 14:38:05

回答

1

您可以指定在選項,這將決定在何處的形式提交到:

<%= form_for @completed_set, url: completed_sets_path do |f| %> 
+1

當我這樣做時,仍然收到「未定義的方法'model_name'爲NilClass:Class」錯誤。 – Arel 2013-05-10 14:29:16

+0

在問題中添加您的「新建」和「創建」操作。 – 2013-05-10 14:31:36

+0

哦,並且錯誤頁面的標題是「用戶#配置文件中的NoMethodError」,所以它仍然在錯誤的控制器中查找@completed_set。我會更新問題 – Arel 2013-05-10 14:32:56

0

在上表單助手可用選項看看,尤其是那些form_for。你可以指定你想要的任何url,如果默認不適合你的情況。

相關問題