【发布时间】:2016-11-29 02:56:32
【问题描述】:
我有一个涉及家谱的项目。首先是问你(当前用户)的问题,然后是关于你父亲的相同问题,然后是关于你母亲的完全相同的问题,一直持续到双方的祖父母。
不幸的是,我尝试了 3 种不同的方法和实现,每次都纠正过去的错误,因为我对 Rails 还很陌生。
我想从这个社区获得关于我在遇到设计问题时应该遵循的最佳设计方法的建议/指导。
所以我认为最好的方法是在我需要将这些信息放在一个单一的家谱中时提出不同的表格以便以后受益。因此,一张表用于当前用户,一张用于父亲、母亲等,每个表都有一个模型,但使用单个控制器,因为我有完全相同的 html.erb 表单,并且每次都会更改标题以适应兄弟问题。
我已成功创建流程和操作,例如新建、创建、显示等,但我的问题如下:
我的问题是连续的,最后显示的是家谱。因此,当用户单击“继续”时,数据将保存在我的数据库的等效表中,并继续流向下一个表。
我被困了 8 个多小时,关于如何使 create 方法从父亲变为母亲,并拥有此代码:
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_Controller 的名称,'new_father' 是同一控制器 (new_father.html.erb) 中的视图。存在其他视图,例如 current_user、new_mother 等。
因此,第一次重定向成功实现,因为数据存储在数据库中(第一次重定向 = 从当前用户转到他的父亲),但是我无法在同一个控制器中从父亲转到母亲,并且表单保持堆叠在 new_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 的同一控制器中执行不同的操作。但是我尝试了一切都没有成功。
有人可以帮助我(首先)关于redirect_to方法的最佳实践,特别是如果我需要更多具有相同方法的控制器或具有不同方法的相同控制器,或者相同的控制器具有相同的功能(我试过这个,我得到了“在单个函数中进行更多重定向或渲染”的错误),其次,对于这种特殊情况,我需要完全相同的字段和操作,但在不同的表中每次都有不同的重定向,最好的设计是什么。
我的路线是:
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 FatherController < ApplicationController
# Creates a new instance of the user object (not a record).
def new_father
@father = User.new
end
# Creates a new record in the database by filling the fields of the new object instance
# with 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
# A private method to pass the parameters in the new object instance.
private
def father_params
params.require(:user).permit(:name, :surname, :dob, :location)
end
# Extracts information from the database.
def show_father
@father = User.find(params[:id])
end
end
- 注意:
我不必建立关系等,重要的是想出一个非常简单的树来显示用户的数据并学习rails,因此关系是一对一、一对多、多对多不重要。
提前谢谢你。
【问题讨论】:
-
请将您的
new_mother和new_father操作代码添加到您的问题中。 -
请同时添加您的路线。
-
感谢您的回复。我已经用您提出的代码更新了问题,但包含关系并不重要,因为这是一个简单的应用程序,主要目标是存在简单树的最终视图。
标签: ruby-on-rails ruby-on-rails-4 model-view-controller controllers