मैं एक ही समय में एक और (नेस्टेड) के साथ एक संसाधन बनाने की कोशिश कर रहा हूं। मैं Rails4 और simple_form 3.0.0rc का उपयोग कर रहा हूँ। मेरा कोड यहाँ है।
मॉडल:घोंसला संसाधनों के लिए रेल और सरल_फॉर्म का उपयोग कैसे करना चाहिए?
class User < ActiveRecord::Base
has_one :profile
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
नियंत्रक:
class UsersController < ApplicationController
def new
@user = User.new
@user.build_profile
end
def create
user = User.new user_params
user.save
redirect_to root_url
# @par =params
end
private
def user_params
params.require(:user).permit(:email, profile_attributes: [:name])
end
end
देखें (नए उपयोगकर्ता के लिए प्रपत्र)
<%= simple_form_for @user do |f| %>
<%= f.input :email %>
<%= simple_fields_for :profile do |p| %>
<%= p.input :name %>
<% end %>
<%= f.submit %>
<% end %>
जब मैं फ़ॉर्म सबमिट करेंगे, कार्रवाई बनाने इस recive पैरामीटर:
{"utf8"=>"✓", "authenticity_token"=>"dJAcMcdZnrtTXVIeS2cNBwM+S6dZh7EQEALZx09l8fg=", "user"=>{"email"=>"[email protected]"}, "profile"=>{"name"=>"Vasily"}, "commit"=>"Create User", "action"=>"create", "controller"=>"users"}
और 'user_params' को कॉल करने के बाद ही बात यह है कि छोड़ दिया
{"email"=>"[email protected]"}
, 'प्रोफ़ाइल' बारे में कुछ नहीं है, इसलिए कोई प्रोफ़ाइल बनाई जाएगी होती है और, जैसा कि आप देख सकते हैं।
मैं क्या गलत कर रहा हूँ?
पीएस मेरी अंग्रेजी के लिए खेद है।
ओच! Thx बहुत @ Bigxiang :) –
यह सचमुच मुझे घंटे बचा लिया। – Kyle