如何重定向用户定义类的输出以在 rails 中查看 [英] How to redirect output of user defined class to view in rails

查看:30
本文介绍了如何重定向用户定义类的输出以在 rails 中查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临以下问题.

我正在 Rails 中制作一个朴素贝叶斯性别预测算法.为此,我已经对其进行了编码并将我的代码文件放在根目录中.我能够访问模型控制器中的类并通过适当调用类来运行代码.

I am making a naive Bayes gender prediction algorithm in rails. For that I have coded it and put my code file in roots directory. I am able to access the class in my Model controller and run the code by appropriate calling of class.

我有一个包含三个字段体重、身高和性别"的表单,当我填写所有这三个字段时,它们应该保存在数据库中.我已经写了它的逻辑,它运行良好.如果我遗漏了性别字段,它应该运行我的代码并呈现适当的视图.

I have a form with three fields "weight, height and gender", when I fill all these three, they should get saved in the db. I have written its logic and it works well. In case I leave out the gender field as it is, it should run my code and render an appropriate view.

在控制器的创建操作中,我正在执行以下操作:

In the create action of controller I am doing the following:

def create
    @user = User.new(params[:user])
    if @user.gender.blank?
       @b_obj= Bayes.new
       @b_obj.predict(@user[height], @user[weight]).
    else
      if @user.save
        redirect_to @user
      else
        @title = "Train"
        render 'new'
      end
    end
  end

此代码运行良好.现在我面临的问题是如何将我的代码的结果(第一个 if 语句)重定向到视图以及在哪里制作它.我的代码返回男性"或女性"作为输出.我应该将它存储在某个变量中吗?我该怎么办?

This code works well. Now I am faced with the problem, how to redirect the result of my code (first if statement) to a view and where to make it. My code returns "male" or "female" as an output. Should I store it in some variable? how should I go about it?

这是我项目中剩下的最后一件事.在尝试了很多方法之后,我终于解决了这个问题,但现在我陷入了僵局.

This is the last thing left in my project. After trying many approaches I have come to this, but now I am in sort of a deadlock.

问候,阿伦

推荐答案

def create
    @user = User.new(params[:user])
    @user.gender ||= Bayes.new.predict(@user[height], @user[weight])
    if @user.save
      flash[:notice] = 'User saved'
      redirect_to @user
    else
      @title = "Train"
      render 'new'
    end
end

这篇关于如何重定向用户定义类的输出以在 rails 中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆