简体   繁体   English

两个模型之间的关系

[英]Relationship between two models

I having difficulties with the @seller_profile.save statement.我在使用 @seller_profile.save 语句时遇到了困难。 Why is it not saving?为什么不保存?

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

You need to Hanlde your else part with Proper error condition handling Simple Put will not do the Job您需要使用正确的错误条件处理来处理您的 else 部分 Simple Put 不会完成这项工作

There are following options to handle it有以下选项可以处理它

1st by ony Render the JSON 1st by on 渲染 JSON

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

2nd redirect back with error in notification第二次重定向返回,通知错误

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

3rd Option to use both Option of Render and Redirect at Same Time第三个选项同时使用渲染和重定向选项

   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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM