Rails 4渲染操作从页面中删除HTML

时间:2022-12-04 23:39:41

I am trying to render the results of a Python script in a rails app while also rendering HTML written in the index.html.erb file. I can render the HTML by redirecting to the index.html.erb file using the redirect_to command (commented out below since it throws an error when used with render). I am able to use the render command to render the result of the Python script. But I need to render both the HTML and the reulst. Here is the controller code:

我试图在rails应用程序中呈现Python脚本的结果,同时还呈现在index.html.erb文件中编写的HTML。我可以使用redirect_to命令重定向到index.html.erb文件来呈现HTML(下面已注释掉,因为它与render一起使用时会抛出错误)。我能够使用render命令来呈现Python脚本的结果。但我需要渲染HTML和reulst。这是控制器代码:

class ContestsController < ApplicationController
  def index
  end

  def new
    @contest = Contest.new
  end

  def create
    @contest = Contest.new(contest_params)

    if contest_params[:contest_url] =~ /\A#{URI::regexp(['http', 'https'])}\z/
      if @contest.save
        value = %x(python test.py #{Shellwords.escape(contest_params[:contest_url])} 2>&1)
        render :text => value
        #redirect_to contests_url
      else
        render 'new'
      end
    else
      render 'new'
    end
  end

  private

  def contest_params
    params.require(:contest).permit(:contest_url)
  end
end

How can I render the HTML in index.html.erb and also render the result of the Python script (saved as value above)?

如何在index.html.erb中呈现HTML并呈现Python脚本的结果(保存为上面的值)?

1 个解决方案

#1


You can't render two different things at the same time. Both render and redirect_to return content back to the requester. You can't return something to the requester twice.

你不能同时渲染两个不同的东西。 render和redirect_to都将内容返回给请求者。您不能两次向请求者返回内容。

What you can do is save the result of the Python script in a variable and pass that variable to one of your views as you render the view. Instance variables in controllers can be passed to ERB templates and rendered as HTML.

您可以做的是将Python脚本的结果保存在变量中,并在渲染视图时将该变量传递给其中一个视图。控制器中的实例变量可以传递给ERB模板并呈现为HTML。

In your controller:

在你的控制器中:

def create
  @contest = Contest.new(contest_params)

  if contest_params[:contest_url] =~ /\A#{URI::regexp(['http', 'https'])}\z/
    if @contest.save
      @value = %x(python test.py #{Shellwords.escape(contest_params[:contest_url])} 2>&1)
      render :index
    else
      render :new
    end
  else
    render :new
  end
end

In your index.html.erb:

在index.html.erb中:

<p>Thanks for submitting! Here's the result of your Python script:</p>
<p><%= @value %></p>

I'm not 100% sure how you want to display this, but this is one solution.

我不是100%确定你想如何显示它,但这是一个解决方案。

#1


You can't render two different things at the same time. Both render and redirect_to return content back to the requester. You can't return something to the requester twice.

你不能同时渲染两个不同的东西。 render和redirect_to都将内容返回给请求者。您不能两次向请求者返回内容。

What you can do is save the result of the Python script in a variable and pass that variable to one of your views as you render the view. Instance variables in controllers can be passed to ERB templates and rendered as HTML.

您可以做的是将Python脚本的结果保存在变量中,并在渲染视图时将该变量传递给其中一个视图。控制器中的实例变量可以传递给ERB模板并呈现为HTML。

In your controller:

在你的控制器中:

def create
  @contest = Contest.new(contest_params)

  if contest_params[:contest_url] =~ /\A#{URI::regexp(['http', 'https'])}\z/
    if @contest.save
      @value = %x(python test.py #{Shellwords.escape(contest_params[:contest_url])} 2>&1)
      render :index
    else
      render :new
    end
  else
    render :new
  end
end

In your index.html.erb:

在index.html.erb中:

<p>Thanks for submitting! Here's the result of your Python script:</p>
<p><%= @value %></p>

I'm not 100% sure how you want to display this, but this is one solution.

我不是100%确定你想如何显示它,但这是一个解决方案。