How to return HTML directly from a Rails controller?

后端 未结 3 469
暗喜
暗喜 2020-12-02 18:00

One of my model objects has a \'text\' column that contains the full HTML of a web page.

I\'d like to write a controller action that simply returns this HTML direc

相关标签:
3条回答
  • 2020-12-02 18:30

    In latest Rails (4.1.x), at least, this is much simpler than the accepted answer:

    def show
      render html: '<div>html goes here</div>'.html_safe
    end
    
    0 讨论(0)
  • 2020-12-02 18:33

    Its works for me

    def show
      @model_object = ModelObject.find(params[:id])
    
       respond_to do |format|
        format.html { render :inline => "<%== @model_object['html'] %>" }
      end
    end
    
    0 讨论(0)
  • 2020-12-02 18:36

    In your controller respond_to block, you can use:

    render :text => @model_object.html_content
    

    or:

    render :inline => "<%= @model_object.html_content %>"
    

    So, something like:

    def show
      @model_object = ModelObject.find(params[:id])
    
      respond_to do |format|
        format.html { render :text => @model_object.html_content }
      end
    end
    
    0 讨论(0)
提交回复
热议问题