开发者

Get content from HTML element in Rails

I'm looking for a way to get the content of my div in my Rails view. I would like the literal HTML content, so only HTML and no Rails

I thought inner_html would work, but I get a RJS exception (TypeError: $("content").innerHtml is not a function)

Does anyone know how I can get the content? Thanks!

In my controller I use this code:

def save_test
    render :update do |page|
        page['content'].innerHTML
    end
end

Some more context maybe? I get a HTML form (without the form tags themselves) and put it into a rails form. What I want is that the user changes checkboxes, radiobuttons, fills out input fields etc and when the user saves, I want the literal changed content of the form as a string to save it to my database.

The reason for this is that the several provided forms differ a lot and it's only used as information, no calculations etc need to be done on it.

So my view looks like this:

form_remote_tag :url=>{:action => 'save_trainee_evaluation' do %>
<div id="content">
  <%= contract_trainer_trainee.evaluation_template %>
</div>
<开发者_开发技巧;%= submit_tag 'Opslaan' %>


The code you are trying to use (i.e. render :update do |page| etc.) is for updating the HTML page (e.g. page['content'].innerHTML = 'Thank you, we received your data.'), not retrieving data from the page (that has to happen in the form submission).

Option 1) Simply use the form field data that has been submitted. (You didn't include an actual example, and I can't tell if there is other non-form content you want to save.) I would tend to do this by converting the params hash to a JSON string and saving that in a text field in the database. E.g. @evaluation.content = params.to_json.

Option 2) Include the full div HTML in the form post by copying it into a hidden field when the form is submitted. (You might need to do some encoding of the HTML string before you put it into the hidden field.) Using your example code as a starting point:

<% form_remote_tag :url=>{:action => 'save_trainee_evaluation'}, :html => { :onsubmit => "$('content_inner_html').value = $('content').innerHTML;" } do %>
<%= hidden_field_tag 'content_inner_html', :id => 'content_inner_html' %>
<div id="content">
  <%= contract_trainer_trainee.evaluation_template %>
</div>
<%= submit_tag 'Opslaan' %>

Then, in the controller, you'll have the HTML block in params[:content_inner_html].


Use page["content"].innerHTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜