ruby on rails - What is the best design practise to create tables from same controller using same actions -
i have project involved genealogy. starts asking questions (current user), same questions father, same questions mother , goes on until grandparents of both sides.
unfortunately, have tried 3 different approaches , implementations, each time correcting past mistakes pretty new rails.
i recommendation/guidance community regarding best design approach should follow having design problems.
so think best way come different tables benefit later when need put info in single family tree. so, 1 table current user, 1 father, mother, etc having model each table using single controller because have same html.erb form , each time changes headers adapt sibling question.
i had created flow , actions such new, create, show etc problem following:
the flow of questions consecutive, , @ end shows family tree. therefore when user clicks continue, data saved in equivalent table of database , flow continues next table.
i stuck more 8 hours on how make create method change father mother having code:
def create @user = user.new(user_params) if @user.save redirect_to :controller => 'users', :action => 'new_father' else render 'new' end end def user_params params.require(:user).permit(:name, :surname, :dob, :location) end
where users name of users_controller , 'new_father' view in same controller (new_father.html.erb). other views exist current_user, new_mother etc.
so, first redirection achieved data stored in database (first redirection = going current user father), can't manage go father mother in same controller , form stays stacked in new_father.html.erb view having following code:
def create @user = user.new(user_params) if @user.save redirect_to :controller => 'users', :action => 'new_mother' else render 'new' end end def user_params params.require(:user).permit(:name, :surname, :dob, :location) end
but need more controllers perform or different action in same controller called create_mother. tried not success.
can please me (firstly) on redirect_to method best practise, particularly if need more controllers same methods or same controller different methods, or same controller same functions (i tried , getting error of "more redirections or renderings in single function") , secondly if best design follow particular situation need same fields , actions in different tables having different redirections each time.
my routes are:
rails.application.routes.draw # first page providing information current user (novice genealogist). 'users/new' # rest of pages asking information form tree. 'fathers/new_father' 'mothers/new_mother' # todo # 'users/new_grandfather_m' # 'users/new_grandmother_m' # input windows user lands on need create new record in database. '/signup', to: 'users#new' '/new_father', to: 'fathers#new_father' '/new_mother', to: 'mothers#new_mother' # todo # '/new_grandfather_m', to: 'users#new' # '/new_grandfather_m', to: 'users#new' # page serve tree showing information above input. '/tree' , to: 'users#show' # used update database creating records above info. post '/signup', to: 'users#create' post '/new_father', to: 'fathers#create_father' post '/new_mother', to: 'mothers#create_mother' # todo # post '/new_grandfather_m', to: 'users#create' # post '/new_grandmother_m', to: 'users#create' # database of our system. resources :users # homepage. root 'users#new' end
the other actions are: (basically had made new controllers each relation)
class fathercontroller < applicationcontroller # creates new instance of user object (not record). def new_father @father = user.new end # creates new record in database filling fields of new object instance # data. def create_father @father = user.new(father_params) if @father.save #redirect_to @user redirect_to :controller => 'mothers', :action => 'new_mother' else render 'new_father' end end # private method pass parameters in new object instance. private def father_params params.require(:user).permit(:name, :surname, :dob, :location) end # extracts information database. def show_father @father = user.find(params[:id]) end end
- note:
i don't have make relations etc, important thing come simple tree show data of user , learn rails therefore relations 1 one, 1 many, many many not important.
thank in advance.
you have 2 create
actions which, following description, reside in different controllers. both try redirect usercontroller
though inconsistent previous. should add controllers , routes code question remove ambiguity.
the new_father
, new_mother
not views of controller actions. when @ end of action:
redirect_to :controller => 'users', :action => 'new_mother'
the browser gets redirect status url should visit next, this:
location: http://localhost:3000/users/new_mother
the browser makes new request @ url, meaning if have routes in place, usercontroller
's new_mother
action executed. new_mother
, new_father
should actions within usercontroller
unless routing hacks. reiterate, in case, happens new request, unrelated first 1 returned redirection status.
when call redirect_to
or render
, rails not yet send response browser, instead marks internal response structure data , goes on rest of action's code. when complete action code has been executed, response sent back. design rails complains if call methods more once within action treats potential mistake in action code.
on design:
there persons , relations in domain. persons have same attributes, user, father or mother or other relative. domain not dictate persons need distributed among different tables. in fact, after relation type between family members. mother, father, sister, nephew, etc. relations between pair of people. make sense present these relations model:
# simplistic representation create_table "relations" |t| t.integer "to_person_id" t.integer "is_person_id" t.string "relation_type" end
relation_type
field carry kinship type string, e.g. 'father'.
although 1 proper way build full-scale genealogy tree application, more complex. involve single-table has-many associations , recursive sql queries, both of rather advanced topics.
there existing gems, handling kind of schema. of them ancestry, acts_as_tree, acts-as-dag.
Comments
Post a Comment