两个模型之间的关系

时间:2020-12-31 19:14:35

标签: ruby-on-rails ruby

我在使用 @seller_profile.save 语句时遇到困难。为什么不保存?

def create 
    @seller_profile = SellerProfile.new(seller_profile_params)
    
    @seller_profile.seller = current_seller
    
    if @seller_profile.save 
     redirect_to home_index_path
    else
      puts "Error"
    end
  end
 def current_seller 
    @current_seller ||= Seller.find(session[:seller_id]) if session[:seller_id]
    
  end

1 个答案:

答案 0 :(得分:0)

您需要使用正确的错误条件处理来处理您的其他部分 简单的 Put 不会完成工作

有以下处理方法

第一个渲染 JSON

   if @seller_profile.save 
     redirect_to home_index_path
   else
     render json: @seller_profile.errors.to_json.to_json
   end

第二次重定向返回通知错误

   if @seller_profile.save 
     redirect_to home_index_path
   else
     flash[:error] = @restaurant.errors
     redirect_to :back 
   end

同时使用渲染和重定向选项的第三个选项

   if @seller_profile.save 
     redirect_to home_index_path
   else
    respond_to do |format|
      format.html { redirect_to :back , flash[:error] = @restaurant.errors }
      format.json { render json: @seller_profile.errors.to_json.to_json }
    end
   end