Ruby on rails 使用相同的操作从同一控制器创建表的最佳设计实践是什么
我有一个涉及家谱的项目。它首先问你一些问题,比如你(当前用户),然后是关于你父亲的问题,然后是关于你母亲的问题,一直问到双方的祖父母 不幸的是,我尝试了3种不同的方法和实现,每次都纠正了过去的错误,因为我对rails还很陌生 我想从这个社区得到一个关于我在设计问题上应该遵循的最佳设计方法的建议/指导 因此,我认为最好的方法是,当我需要将这些信息放在一个族谱中时,列出不同的表格,以便以后从中受益。因此,一个表用于当前用户,一个表用于父亲、母亲等,每个表都有一个模型,但使用一个控制器,因为我有完全相同的html.erb表单,每次都更改标题以适应同级问题 我已经成功创建了流程和操作,如新建、创建、显示等,但我的问题如下: 我的问题是连续的,最后显示了一个家谱。因此,当用户单击continue时,数据将保存在my database的等效表中,流将继续到下一个表 我花了8个多小时研究如何使创建方法从父亲更改为母亲,代码如下:Ruby on rails 使用相同的操作从同一控制器创建表的最佳设计实践是什么,ruby-on-rails,ruby-on-rails-4,model-view-controller,controllers,Ruby On Rails,Ruby On Rails 4,Model View Controller,Controllers,我有一个涉及家谱的项目。它首先问你一些问题,比如你(当前用户),然后是关于你父亲的问题,然后是关于你母亲的问题,一直问到双方的祖父母 不幸的是,我尝试了3种不同的方法和实现,每次都纠正了过去的错误,因为我对rails还很陌生 我想从这个社区得到一个关于我在设计问题上应该遵循的最佳设计方法的建议/指导 因此,我认为最好的方法是,当我需要将这些信息放在一个族谱中时,列出不同的表格,以便以后从中受益。因此,一个表用于当前用户,一个表用于父亲、母亲等,每个表都有一个模型,但使用一个控制器,因为我有完全相
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
其中users是users\u Controller的名称,“new\u father”是同一控制器中的视图(new\u father.html.erb)。其他视图存在,如当前用户、新用户等
因此,当数据存储在数据库中时,成功地实现了第一次重定向(第一次重定向=从当前用户转到其父亲),但是我无法在同一个控制器中从父亲转到母亲,表单仍然堆叠在新的_father.html.erb视图中,具有以下代码:
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
但是,我需要更多的控制器来在同一个名为create_mother的控制器中执行此操作或不同的操作。但我什么都试过了,但没有成功
有人能(首先)帮助我重定向到方法最佳实践吗,特别是如果我需要更多控制器使用相同的方法,或者相同控制器使用不同的方法,或者相同控制器使用相同的功能(我尝试了这个,我得到了“在单个功能中进行更多重定向或渲染”的错误)其次,对于这种特殊情况,我需要完全相同的字段和操作,但是每次在不同的表中有不同的重定向,那么最好的设计是什么呢
我的路线是:
Rails.application.routes.draw do
# The first page providing information about the current user (novice genealogist).
get 'users/new'
# The rest of the pages asking information to form the tree.
get 'fathers/new_father'
get 'mothers/new_mother'
# TODO
# get 'users/new_grandfather_m'
# get 'users/new_grandmother_m'
# The input windows that the user lands on need to create a new record in the database.
get '/signup', to: 'users#new'
get '/new_father', to: 'fathers#new_father'
get '/new_mother', to: 'mothers#new_mother'
# TODO
# get '/new_grandfather_m', to: 'users#new'
# get '/new_grandfather_m', to: 'users#new'
# This page will serve as the tree showing information from the above input.
get '/tree' , to: 'users#show'
# Used to update the database by creating records with the 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'
# The database of our system.
resources :users
# The homepage.
root 'users#new'
end
其他操作是:(基本上,我为每个关系创建了新的控制器)
class父控制器'母亲',:操作=>'新母亲'
其他的
呈现“新父亲”
结束
结束
#在新对象实例中传递参数的私有方法。
私有的
我的父亲
参数require(:user).permit(:name,:姓氏,:dob,:location)
结束
#从数据库中提取信息。
爸爸
@父=用户.find(参数[:id])
结束
结束
- 注:
提前感谢。您有两个
创建
操作,按照您的描述,它们驻留在不同的控制器中。两者都尝试重定向到UserController
,尽管这与前面的不一致。您应该在问题中添加控制器和路由代码,以消除歧义
新父亲
和新母亲
不是控制器的视图,而是操作。当你在一个动作结束时做这样的事情:
redirect_to :controller => 'users', :action => 'new_mother'
浏览器将获得重定向状态,其中包含下一步应访问的url,如下所示:
Location: http://localhost:3000/users/new_mother
然后,浏览器在该url上发出新请求,这意味着如果您的路由已就绪,您的UserController
的new\u-mother
操作将被执行。你的新母亲
和新父亲
应该是你的用户控制器
中的动作,除非你做了一些路由黑客。重申一下,以防万一,这是一个全新的请求,与第一个返回重定向状态的请求无关
当您调用redirect\u to
或render
时,Rails尚未将响应发送到浏览器,而是使用一些数据标记其内部响应结构,并继续执行操作的其余代码。只有在执行完完整的操作代码后,才会返回响应。根据Rails的设计,如果您在操作中多次调用这些方法,Rails会抱怨,因为它将此视为操作代码中的潜在错误
关于设计: 在你的领域里有人和关系。人具有相同的属性,无论是用户、父亲或母亲或任何其他亲属。您的域并不规定人员需要分布在不同的表中。事实上,你主要追求的是家庭成员之间的关系类型。母亲、父亲、姐姐、侄子等是一对人之间的关系。因此,将这些关系作为一个模型来呈现是有意义的:
# simplistic representation
create_table "relations" do |t|
t.integer "to_person_id"
t.integer "is_person_id"
t.string "relation_type"
end
relationship\u type
字段将以字符串形式携带亲属关系类型,例如“父亲”
虽然这将是一个正确的方式来建立一个全面的genea
# simplistic representation
create_table "relations" do |t|
t.integer "to_person_id"
t.integer "is_person_id"
t.string "relation_type"
end
class User < ActiveRecord::Base
has_many :answers
belongs_to :mother , class_name: 'User' , foreign_key: 'mother_id' #This is telling the user belongs to a mother and it's stored in same table. And mother reference will be stored in mother_id column. So user table should have a column mother_id
belongs_to: father , class_name: 'User', foreign_key: 'father_id' #This is telling the user belongs to a father and it's stored in same table. And father reference will be stored in father_id column. So user table should have a column father_id
has_many :children ,->(user){ where("users.father_id ? or users.mother_id = ?",user.id)} #Every user can have many children. Whether it's a mother or a father
def grand_father_from_father_side
father && father.father
end
def grand_father_from_mother_side
mother && mother.father
end
#Similary you can get grammy like mother.mother
end
class Question < ActiceRecord::Base
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
#There will be a question_for column in answer table which will tell you this answer was posted for which relation i-e Mother , Father or User Himself
end