ruby - Nested registration data in Rails 3.1 with Devise -
ruby - Nested registration data in Rails 3.1 with Devise -
hello again, stackoverflow community!
i'm working on writing simple blog scheme in rails, , i'm using devise authentication part.
i got basic email/password registration going, , i've set one-to-one connection of users table (the basic 1 generated devise) , userdata table, containing things such username, permissions, about, , on.
it's pretty easy - user table 100% devise vanilla, , has_one userdata. userdata belongs_to user.
i tried expanding sign form generated devise, allow user enter, not e-mail , password, desired user name. way, when form sent, if names of form fields right (the right construction of nested hashes, such "email" user's email, , "userdata[name]" user's desired name), new user created automatically, , proper corresponding userdata entry should created automatically rails, correct?
right now, i'm having huge issue userdata[name] field not appearing @ all... here's file i'm having problem with...
new.html.erb<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) |f| %> <%= devise_error_messages! %> <div><%= f.label :email %><br /> <%= f.email_field :email %></div> #this tidbit doesn't work <%= f.fields_for "userdata" |ud_f| %> <%= ud_f.label "username" %> <%= ud_f.text_field :name %> <% end %> <div><%= f.label :password %><br /> <%= f.password_field :password %></div> <div><%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation %></div> <div><%= f.submit "sign up" %></div> <% end %> <%= render :partial => "devise/shared/links" %>
i have been searching reply hours , hours, no avail. if purposely misspell word "userdata" in fields_for block, prints out field. strange, , have no thought what's making field disappear...
i'd appreciate little bit of help! time! :)
edit 1+2:after few messing around, i've overridden devise's default registration controller , created own. i've edited devise route phone call custom controller timbrandes suggested, can't seem access @user in controller. here's code have far...
class registrationscontroller < devise::registrationscontroller def new resource = build_resource({}) resource.userdata = userdata.new respond_with resource end def create logger.debug "\n\n\nin create\n\n\n\n" logger.debug params.to_json super end def update super end end
well, lot of time has passed, got working scrapping , re-doing of devise code. honest, wasn't much re-write, , works great.
for future developers browsing (slightly confusing) question , answer, advice can give create sure write structured form code, nested resources. current registrations_controller.rb
works:
class registrationscontroller < devise::registrationscontroller def new resource = build_resource({}) resource.build_user_data respond_with resource end def create resource = build_resource(params[:user]) if(resource.save) sign_in(resource_name, resource) respond_with resource, :location => after_sign_up_path_for(resource) else render :action => "new" end end def update super end end
and .erb registration form:
<div class="form-box"> <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) |f| %> <%= devise_error_messages! %> <div><%= f.label :email %> <%= f.email_field :email %></div> <%= f.fields_for(:user_data) |udf| %> <%= udf.label :name, "desired user name" %> <%= udf.text_field :name %> <%= udf.label :roles, "privileges"%> <% role in userdata::roles %> <%= check_box_tag "user[user_data_attributes][roles][]" , role, resource.user_data.roles.include?(role) %> <%=h role.humanize %><br /> <% end %> <%= hidden_field_tag "user[user_data_attributes][roles][]" %> <% end %> <div><%= f.label :password %> <%= f.password_field :password %></div> <div><%= f.label :password_confirmation %> <%= f.password_field :password_confirmation %></div> <div><%= f.submit "sign up" %></div> <% end %> <%= render :partial => "devise/shared/links" %> </div>
ruby-on-rails ruby
Comments
Post a Comment