ruby-on-rails - 如何修复 ruby 中丢失的模板配置文件错误
问题描述
我正在建立一个系统,用户可以在其中制作个人资料并填写信息。如果我遵循表单验证并输入正确的输入,它会被接受并且不正确的输入会被拒绝,但是当我单击编辑配置文件并将其更改为不正确的内容(如空白名字)时,它会给我一个缺少模板配置文件错误
这是我的个人资料控制器
class ProfilesController < ApplicationController
before_action :authenticate_user!
before_action :only_current_user
# GET to /users/:user_id/profile/new
def new
# Render blank profile details form
@profile = Profile.new
end
# POST to /users/:user_id/profile
def create
# Ensure that we have the user who is filling out form
@user = User.find( params[:user_id] )
# Create profile linked to this specific user
@profile = @user.build_profile( profile_params )
if @profile.save
flash[:success] = "Profile updated!"
redirect_to user_path(id: params[:user_id] )
else
render action: :new
end
end
def edit
@user = User.find( params[:user_id] )
@profile=@user.profile
end
def update
@user = User.find( params[:user_id] )
@profile = @user.profile
if @profile.update_attributes(profile_params)
flash[:success] = "Profile Updated."
#redirect to their profile page
redirect_to user_path(id: params[:user_id] )
else
render action: edit
end
end
private
def profile_params
params.require(:profile).permit(:first_name, :last_name, :avatar, :age, :gender, :collegeemail)
end
def only_current_user
@user = User.find( params[:user_id] )
redirect_to(root_url) unless @user == current_user
end
end
这是我的表单验证所在的 profile.rb。
class Profile < ActiveRecord::Base
belongs_to :user
validates :first_name, presence: true
validates :last_name, presence: true
has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => ":style/missing.jpg"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
def checkemail?
if self.collegeemail.present?
domain = self.collegeemail.split("@").second
return if Swot::is_academic?(self.collegeemail) && Swot::is_academic?(domain)
end
errors.add(:collegeemail, 'not academic email')
end
validate :checkemail?
end
这是确切的错误
Missing template profiles/#<Profile:0x007f014c6fff28>, application/#<Profile:0x007f014c6fff28> with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/ec2-user/environment/saasapp/app/views" * "/home/ec2-user/.rvm/gems/ruby-2.3.0@saasapp/gems/devise-4.2.0/app/views"
Extracted source (around line #38):
36 redirect_to user_path(id: params[:user_id] )
37 else
38 render action: edit
39 end
40 end
解决方案
Rails 正在尝试查找不存在的模板
也许尝试改变
render action: edit
至
render action: :edit
推荐阅读
- ssl - TLS 错误:TLS 密钥协商未能在 60 秒内发生(检查您的网络连接)
- time-complexity - 用更正的文本识别和替换乱码文本的高效算法
- javascript - 从 javascript 中的 for 循环结果创建关联数组
- javascript - 列表卡未在条带中检索
- python - If-Else not working in Tkinter under functions请解决这个问题:
- javascript - 将 2 个按钮合并为 1 个
- stata - 如何在Stata中没有J变量的情况下将特定数据集从长到宽重塑?
- linux - 如何使用linux命令“convert -draw”更改添加在图像上的文本的大小
- mysql - 如何处理数据库中的好友请求接受逻辑?
- reactjs - 从 Formik 字段中提取值并执行自定义 onChange 函数