我認爲這非常簡單,但無法找到任何關於如何操作的解釋。在一個視圖中創建一個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
*在Google上的* ruby rails形式,第一個結果:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for ... – Raindal 2013-05-10 14:27:19
感謝您分享您的谷歌搜索結果與我,但這並不能回答我的問題。 – Arel 2013-05-10 14:34:27
在一個視圖中創建一個form_for **,併發布到另一個視圖的控制器**,我的「google結果」告訴您<%= form_for @post,:as =>:post,:url => posts_path ... '如果只是花時間向下滾動並閱讀並看到url屬性 – Raindal 2013-05-10 14:38:05