ruby on rails - Parameter not being passed to partial -
i have rails 4 application user accounts can created in 2 ways: new users (sign up) or admin users (add account).
my thought process send requests new action in user controller, have logic in there see if it's new user or admin user , handle request accordingly. here's template:
new_by_admin.html.erb:
<%= form_for(@user) |f| %> <div class="col-md-6 col-md-offset-3"> <h1 class="centertext">add user</h1> <%= render 'add_user_fields', f: f %> <%= f.submit "create account", class: "btn btn-large btn-primary" %> </div> <% end %>
in controller, works fine:
user_controller.rb:
def new @user = user.new if !signed_in? @user.confirmation_code = user.new_confirmation_code elsif signed_in? && current_user.admin? new_by_admin else redirect_to root_url end end def new_by_admin @user = user.new end
however, below code throws "first argument in form cannot contain nil or empty" error in template, presumably because @user nil.
def new @user = user.new if !signed_in? @user.confirmation_code = user.new_confirmation_code elsif signed_in? && current_user.admin? render 'new_by_admin', user: @user else redirect_to root_url end end
what difference between these, , why doesn't second method work though i'm passing @user object parameter partial?
render
allows instruct contollers action render different view default matches action name (new
in case). when specified view rendered has access of instance vars set action. there's no need pass param (i'm not sure that's possible).
instead, use: render :new_by_admin
Comments
Post a Comment