Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Amazingly, after reading a blog post this evening, and some more trial and error, I worked this out myself.</p> <p>You need to assign an <code>@user</code> variable in the 'new' action so that the user params are available for use in the 'create' action. You then need to use both the <code>@account</code> and <code>@user</code> variables in the view.</p> <p>The changes look like this.</p> <p>Accounts Controller:</p> <pre><code>def new @account = Account.new @user = @account.users.build end def create @account = Account.new(params[:account]) @user = @account.users.build(params[:account][:user] if @account.save flash[:success] = "Welcome." redirect_to @account else render :new end end </code></pre> <p>The accounts/new view changes to:</p> <pre><code>= simple_form_for @account do |f| = f.input :name = f.simple_fields_for [@account, @user] do |u| = u.input :email = u.input :password = u.input :password_confirmation = f.button :submit, :value =&gt; "Sign up" </code></pre> <p>In this case the params remain nested but have the <code>user</code> component explicitly defined:</p> <pre><code>:account {"name"=&gt;"In", "user"=&gt;{"email"=&gt;"user@example.com", "password"=&gt;"pass", "password_confirmation"=&gt;"pass"}} </code></pre> <p>It has the additional side effect of removing the <code>@account.users.build</code> from within the <code>else</code> path as @numbers1311407 suggested</p> <p>I am not certain whether their are other implications of this solution, I will need to work through it in the next few days, but for now I get the information I want defaulted into the view in the case of a failed <code>create</code> action.</p> <p>@Beerlington and @numbers1311407 I appreciate the help in guiding me to the solution.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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