0

im new for ruby on rails. i have 3 question

  1. how to i translate the html code

in controllers i set

def index
  @sentence = "Halo<br>worlds";
end

in index.html.erb display i set

<%= @sentence %>

but display

Halo<br>world

how do i make it like this

halo
world
  1. how do i set onclick in submit button? i already tried set the button like this <%= submit_tag "back", onclick: "window.history.back();" %>

but the result is not go to previous page. but excetude the form..

  1. how i can make an validation form if the validation code is in controllers. not in model
Ferdinand
  • 107
  • 2
  • 11
  • 1
    Technically you can use `html_safe` e.g. `<%= @sentence.html_safe %>` but I would not recommend this as marking strings as html safe can lead to code injection depending on how you are utilizing this. Sidenote: the proper break tag should be `
    `. As for the other questions these are really 3 **very** separate questions (rails rendering, javascript, and server side code) and I would recommend doing a little research and then if you still cannot figure it our come back and ask them as separate questions.
    – engineersmnky Jul 16 '18 at 15:50
  • I tested your question 2 on Rails 5.1, and it works as it's supposed to (browser goes back). Could be a browser issue. You should post it as a separate question. Don't write more than one question per post. – Casper Jul 16 '18 at 16:31

2 Answers2

1

Rails automatically escapes all HTML in ERB templates. To prevent this escaping you can use the Rails ERB extension <%== %>. So in your case it would be:

<%== @sentence %>

See this question for some further discussion:
What does <%== %> do in rails erb?

Casper
  • 33,403
  • 4
  • 84
  • 79
  • 1
    Wow. I didn't know about `<%==` (although honestly now that I do it terrifies me :)) I would certainly prefer the explict nature of `raw` or `html_safe` over this little gem, but thank you for teaching me something. – engineersmnky Jul 16 '18 at 15:56
  • True. It's a good idea to point out that it's potentially unsafe to use any of the unescaping methods in your templates. Thanks. – Casper Jul 16 '18 at 15:59
0

Replace <%= @sentence %> with below content for a simple solution.

<% @sentence.split("<br/>").each do |word| %>
 <%= word %><br/>
<% end %>

for the second qsn, check the validations in validateform() function and then respond with true or false accordingly.

<%= button_to_function "Submit", "validateform()" %>
Bijendra
  • 9,467
  • 8
  • 39
  • 66