【发布时间】:2015-06-22 23:36:04
【问题描述】:
对于两种不同的注册表单,我有两个部分视图。在我的主页上,根据单击的链接,我正在呈现相应的表单。(视图/应用程序/索引)
= link_to 'Mentor', new_user_path(user_role: true), :class =>'btn'
= link_to 'Mentee', new_user_path, :class =>'btn'
在 views/users/new.html.haml 中,我正在检查用户角色并重定向到相应的表单。
- if params[:user_role]
= render 'mentor'
- else
= render 'mentee'
在用户模型中,我添加了这样的验证。
class User < ActiveRecord::Base
email_regex = /\A[\w+\-.]+@cisco.com/i
validates :cisco_email, :presence => true,
:format => { :with => email_regex,}
validates :work_city, :presence => true
end
因此,当有任何无效字段时,我想通过一条消息将其定向到同一个表单。我的控制器看起来像这样。
class UsersController < ApplicationController
def index
end
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user]) # Not the final implementation!
if @user.save
flash[:success] = "Welcome to the CSG Mentoring Tool!"
redirect_to @user
else
flash[:notice] = "Error regsitering."
if params[:user][:user_role]
render :partial => 'users/mentor'
else
render :partial => 'users/mentee'
end
end
end
end
当存在无效字段条目时,无论在哪个页面发生错误,它都会重定向到“受指导者”页面。整个css样式也改变了,flash也没有显示
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 model-view-controller