0

If I have a view that is rendered with the following element:

View

<div id="content">
    <!-- More HTML -->
</div>

In my controller, is there such a method where I could retrieve the HTML string of a specific element using its id? For example, it might look something like:

def myview

    contentString = get_html_element_string("content")

    ...
end

where contentString would contain the value <div id="content"> <!-- More HTML --> </div> as a string.

I understand you can fetch the HTML of the whole rendered page using render_to_string, but I'd rather not parse through that.

Nick Boukis
  • 55
  • 1
  • 4
  • A controller is actually list of actions. So, one of the way you can pass values of your elements is using forms with post method where your submitted form passes params into your action. Those arrived params your simply catch and use by params[:something_from_your_form]. I am not sure what you are trying to do, may be there is no need to pass into your actions and further to backend, amy be there is something that you still want to keep in your frond-end layer, thus you can better use jQuery or so.. – evgeny Jun 07 '16 at 21:54

2 Answers2

0

It's not really possible to access the rendered content after the fact in the controller.

I would recommend creating a helper function to create the content that goes inside the content div. Then call the helper in the myview if you need it there.

The following post gives some options for calling a helper within a controller: Rails - How to use a Helper Inside a Controller

Community
  • 1
  • 1
Fred Willmore
  • 4,386
  • 1
  • 27
  • 36
0

I actually ended up putting the HTML I need within a partial and rendering it from within the controller (and again later in my view using <%= render partial: "content_partial" %>).

_content_partial.html.erb

<div id="content">
    <!-- More HTML -->
    <%= @my_value.function %>
</div>

And within my controller:

def myview

    contentString = render_to_string(:partial => 'content_partial', :layout => false, :locals => {:my_object => @my_value})

    ...
end
Nick Boukis
  • 55
  • 1
  • 4