Ruby on rails 如何让控制器通过if-else语句呈现不同的表单?
我正在尝试重新配置密码重置功能,以便将其用于其他型号。除了一个问题外,我的一切都在进行中 它可以通过搜索两个表来找到用户。然后它发送一个链接,如http://url/password_resets/Uhhi7Omp***pigTtg/edit?email=main%40example.com 但是,根据用户来自哪个表,它应该呈现不同的表单。在控制器中,我刚刚有:Ruby on rails 如何让控制器通过if-else语句呈现不同的表单?,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我正在尝试重新配置密码重置功能,以便将其用于其他型号。除了一个问题外,我的一切都在进行中 它可以通过搜索两个表来找到用户。然后它发送一个链接,如http://url/password_resets/Uhhi7Omp***pigTtg/edit?email=main%40example.com 但是,根据用户来自哪个表,它应该呈现不同的表单。在控制器中,我刚刚有: def edit end 但是我现在有两个视图,每种类型的用户/表一个。我在控制器中尝试了以下操作: def edit
def edit
end
但是我现在有两个视图,每种类型的用户/表一个。我在控制器中尝试了以下操作:
def edit
if @type1 #This if statement works, also use it for other methods in the controller.
render _to edit_type1 #Name of the view file
elsif @type2
render reset_type1 #Name of the view file
end
end
但由于路由不知道edit_type1和type2,因此这不起作用。我应该向routes文件添加什么?由于视图没有自己的控制器方法,我不确定
路线:
resources :password_resets, only: [:new, :create, :edit, :update]
这与路由无关-它将请求发送到正确的控制器和操作,因此它的工作将完成,直到您需要生成更多URL为止 这应该是
render action: "edit_type1"
如果要创建其他管线,可以执行以下操作
resources:password_resets do
member do
get 'edit_type_1', to: 'my_controller#type_1_incoming, as: 'edit_type1'
get 'edit_type_2', to: 'my_controller#type_2_incoming, as: 'edit_type2'
end
end
或
成员和集合之间的区别在于是否需要传递id来指定特定对象
。。。但我不认为你需要根据我对你的要求的理解来修改你的路线。是否要显示不同的表单,但使用相同的更新方法
那么两种形式都应该有这样的东西
<%= form_for :password_reset do |f| %>
表单将自动提交给密码重置控制器,更新方法。更改路由到资源:密码重置,仅:[:新建,:创建,:编辑类型1,:编辑类型2,:更新]不起作用。我不确定这为什么不能为edit_type1和edit_type2创建路径。
<%= form_for :password_reset do |f| %>