我正在尋找一種解決方案,以便在整個窗體的導航之前對嵌套資源進行預先驗證,並且對正確的設計沒有任何線索。嵌套窗體中的多個動作 - 傳遞給屬性link_to
所以我有has_one :place
正在接受嵌套屬性的簡單User
模式:
class User < ActiveRecord::Base
...
has_one :place, :dependent => :destroy
accepts_nested_attributes_for :place
attr_accessible :place_attributes
...
end
的Place
模型包含的屬性,如:street_number, :street, :postal_code, :city, :country
。
我想設置一個窗體用於編輯用戶,以便他可以介紹這個地方。提交之前,我想讓用戶有機會驗證該地點。所以我在PlaceController
中設置了一個自定義操作。
# place_controller.rb
class PlaceController < ApplicationController
...
def validate
# code for validation
end
end
順便說我定義該地點的路線如下:
# route.rb
resources :users do
resource :place do
match 'validate', :to => 'place#validate'
end
end
然後在視圖我設置的形式:
<%= form_for(:user, :url => edit_user_path(@user), :html => {:method => :put, :multipart => true}) do |f| %>
<%= f.text_field :name %>
# other fields for users
...
<%= f.fields_for :place do |builder| %>
<%= render 'places/form', :f => builder %>
<% end %>
<%= f.submit "Update" %>
和部分places/form
管理嵌套屬性的字段place
:
<%= f.text_field :street_number %>
<%= f.text_field :street %>
...
這裏是要點:我想要提交或鏈接調用validate
動作與地點模型的屬性。 我想是這樣的:
<%= link_to 'Validate', validate_user_place_path(@user, :format => :js, :params_to_validate => f.object), :remote => true %>
即使它調用正確的控制器,我沒有得到的屬性在控制器進行驗證。
我該怎麼辦?
感謝您的幫助!