What is the Ruby way of integrating a lookup table into a form? -



What is the Ruby way of integrating a lookup table into a form? -

i new rails. know there similar questions out there on this, cannot find hits i'm looking for. have "roles" lookup table in ror app stores type of roles user can have, e.g "admin", "super admin", "user" etc. right way managing when user created have radio boxes given roles this:

<%= f.label :role %> <%= f.radio_button :role, 2%> super user <%= f.radio_button :role, 3 %> admin <%= f.radio_button :role, 4 %> user

these numbers, 2,3,4 correspond ids in role table

and in users_controller do:

def create @user = user.new @user.email = user_params[:email] @user.firstname = user_params[:firstname] @user.lastname = user_params[:lastname] @user.password = user_params[:password] @user.password_confirmation = user_params[:password_confirmation] @user.role = role.find(user_params[:role]) if @user.save flash[:success] = "welcome sample app!" redirect_to @user else render 'new' end end

because doing "@user = user.new(user_params)" throws error correctly informing me "role" expected, not int.

even novice eyes can see though works not right , wondering if tell me proper way it.

thanks!

as described in this answer, can add together radio buttons role table follows:

<% role.all.each |r| %> <%= f.radio_button :role_id, r.id %> <%= f.label :role_id, r.name %> <% end %>

then, need permit :role_id rather :role in user_params, , should go.

class usercontroller < applicationcontroller def create @user = user.new(user_params) if @user.save flash[:success] = "welcome sample app!" redirect_to @user else render 'new' end end private def user_params # permit :role_id params.require(:user).permit(:email, :firstname:, :lastname, :role_id, :password, :password_confirmation) end

ruby-on-rails ruby coding-style

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -