Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do this with accepts_nested_attributes_for and nested models.</p> <p>Simple checkout</p> <ul> <li><a href="http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html" rel="nofollow">http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html</a></li> <li><a href="http://railscasts.com/episodes/196-nested-model-form-part-1" rel="nofollow">http://railscasts.com/episodes/196-nested-model-form-part-1</a></li> <li><a href="http://railscasts.com/episodes/197-nested-model-form-part-2" rel="nofollow">http://railscasts.com/episodes/197-nested-model-form-part-2</a></li> <li><a href="http://railscasts.com/episodes/196-nested-model-form-revised" rel="nofollow">http://railscasts.com/episodes/196-nested-model-form-revised</a></li> <li><a href="http://railscasts.com/episodes/403-dynamic-forms" rel="nofollow">http://railscasts.com/episodes/403-dynamic-forms</a></li> </ul> <p><strong>UPDATE:</strong></p> <p>add the shop attributes to the form (with fields_for => accepts_nested_attributes_for), hide these form fields. create an attr_accessor in your user model, for example called :create_shop_now, add this to your form. if :create_shop_now is checked, show the shop form fields..an user can fill in the fields. user submits the form to the create action. The create action could look like following:</p> <pre><code>def create build_resource if !params[:user][:create_show_now] params[:user].delete(:shops_attributes) end if resource.save if resource.active_for_authentication? set_flash_message :notice, :signed_up if is_navigational_format? sign_up(resource_name, resource) respond_with resource, :location =&gt; after_sign_up_path_for(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? expire_session_data_after_sign_in! respond_with resource, :location =&gt; after_inactive_sign_up_path_for(resource) end else clean_up_passwords resource respond_with resource end end </code></pre> <p><strong>UPDATE2</strong></p> <p>Remove your after_initialize hook in the model and update your registrations#new action to following:</p> <pre><code>def new resource = build_resource({}) resource.shops.build respond_with resource end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload