首页 > 解决方案 > 如何修复 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



标签: ruby-on-rails

解决方案


Rails 正在尝试查找不存在的模板

也许尝试改变

render action: edit

render action: :edit

推荐阅读