Assigning roles and menu items at signup

My problem is similar to the thread from the rails forum:

Creating Two Models in One Form
http://railsforum.com/viewtopic.php?id=717

I am using acts_as_authenticated and role_requirement and a model called
“menu” all in one controller “Account” (from acts as authenticated). For
various reasons I wanted menu items to be different than roles. I have
everything working on a basic level.

What I want to do is assign a set of menu items and roles upon signup.

The attempt below will create a new menu when signing up (not what I
want to
do). Ideally I could get the controller to look at a field in the
database
for each table and see if it matched “default” or some other value.

def signup
@user = User.new(params[:user])
@menu = @user.menus.build(attributes={:id => 1})
return unless request.post?
@user.save!
self.current_user = @user
redirect_back_or_default(:controller => ‘/welcome’, :action =>
‘index’)
flash[:notice] = “Thanks for signing up!”
rescue ActiveRecord::RecordInvalid
render :action => ‘signup’
end

I was even thinking about some kind of “after_save()” action that would
assign menu items and roles while the user gets a “thanks for signing
up”
page. I have not used this before so i am not exactly sure how to use
it.

Any advice would be appreciated.

Sunny

@user.is_owner_of @menu
This will create Role with @user having @menu,
then:
return true if @user.is_owner_of @menu

OR

you can make:
@user.has_role “menu_owner”
@menu.accept_role “menu_owner”
This will make new role for @user naming “menu_owner” and make @menu
accept ANY user having “menu_owner” permission.
then:
if @user.is_menu_owner_of @menu do {}

This is works well.

I wanted to go ahead and post my solution for this thread of
questions.

In this scenario the menus table has a column called “kind” and some
of the records are named “default”.

def signup
@user = User.new(params[:user])
return unless request.post?
@user.save!
self.current_user = @user
current_user.menus = Menu.find_all_by_kind(‘default’)
redirect_back_or_default(:controller => ‘/welcome’, :action =>
‘index’)
flash[:notice] = “Thanks for signing up!”
rescue ActiveRecord::RecordInvalid
render :action => ‘signup’
end

Sunny

Thanks for the suggestion but I am not able to get it to work right.

I am going to try to re-word the original thought:

When someone signs up I would like to save the information they are
sending (params[:user]) and add a relationship to an existing menu (in
a has_and_belongs_to_many situation).

So when user xxx signs up (and becomes id=5) there is an entry in the
menus_users table that is (menu_id 1/user_id 5). This menu (id=1)
already exists.

Thanks,
Sunny

On Sep 9, 5:42 am, “alexey.Creopolis” [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs