cant translate html code in ruby on rails - ruby-on-rails

im new for ruby on rails. i have 3 question
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
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..
how i can make an validation form if the validation code is in controllers. not in model

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?

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()" %>

Related

Redirect to new view with submit_tag in Rails

I have the following submit_tag button
<%= submit_tag("Save Email".upcase, name:"email_change","data-target":"email_change.submit") %>
I am trying to have it redirect to a new view, similar to what is done with using link_to. Is there a way to do something similar using the submit button?
You can do:
<% link_to some_path do %>
<%= submit_tag("Save Email".upcase, name:"email_change","data-target":"email_change.submit") %>
<% end %>
The 'Rails' way to do this would probably be to wrap the redirect in some conditional logic in your controller action, especially if you're submitting data from a form

I searched passing params in a tags in your views but I did not find anything that can help me

I am building a simple Rails app that has Users, Blogs, and Comments. The last thing I have to do is make a link on the blogs show page where comments show and have those links send the user to the comment/:id/edit page where they can edit their comment.
However, I'm not able to grab the comment ID correctly for some reason and even though I am getting thrown no errors I am not brought to the error form, just a blank web page that has my layout elements.
Here is my blogs controller's show action:
def show
#blog = Blog.find_by_id(params[:id])
#comment = Comment.new
# #current_comment = Comment.find(params[:id])
end
Here is my comments controller's edit action:
def edit
#comment = Comment.find_by_id(params[:id])
end
And here is where the link is in the Blogs show view:
<% #blog.comments.each do |c| %>
<div><%= c.content %></div>
<%= link_to 'Delete', "/comments/#{c.id}", method: :delete %>
<% end %>
The delete function works, but when I try to grab the comment id in the same way for the link to the comment view it does not work. Instead of seeing a number in the url like 1, 2, or whatever the comment id is, I see the #{c.id} syntax. Any thoughts?
<% #blog.comments.each do |c| %>
<div>
<%= link_to c.content, edit_comment_path(c) %>
</div>
<%= link_to 'Delete', c, method: :delete %>
<% end %>
In general you want to avoid hardcoding paths when possible as it makes your code more brittle and verbose.
As you have written it "/comments/#{c.id}/edit" is not even being evaluated since its not inside a ERB tag.
I suppose you mean this part:
<div><%= c.content %></div>
That's because the <div><a href="/comments/#{c.id}/edit"> part is outside a ERB block, therefore it is being interpreted as plain HTML, and that's what you see in the url. There's no variable expansion in plain HTML inside a ERB file.
Either use link_to:
<div><%= link_to c.content, "/comments/#{c.id}/edit" %></div>
or enclose the url part in ERB tags:
<div><%= c.content %></div>

Ruby on Rails - Underline words if they appear in dynamically generated text using Ruby

I am trying to underline words that are dynamically generated by the debug(params) method provided by rails. I have something below, but it obviously does not work, plus what I have below is attempt to try and change the words using methods that I already know about (like the .upcase method). I was hoping to underline the word controller if it appears in the text using only Ruby. Can anyone help me out here?
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<%= 'controller'.upcase %>
<% end %>
thanks
edit:
I should add that debug(params) is a method defined by RAILS, I was able to do the following which seems even more off, so far the answers have not been correct to what I want to do.
<% if Rails.env.development? %>
<% debug_method = debug(params).split.each do |word| %>
<% if word == 'controller:' %>
<ul><% word.upcase %></ul>
<% end %>
<% end %>
<%= debug_method.join %>
<% end %>
which returns the following text: https://ibb.co/cvnEpw , keep the answers coming in though. I want to get the words in the original box (that's generated by the method to underline the controller word https://ibb.co/jmSm2G).
use <u></u> tag
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<u><%= 'controller'.upcase %></u>
<% end %>
example here
Provide the css to generate html element:
p { text-decoration: underline; }
Add html elemnt to wrap your words:
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<p> <%= 'controller'.upcase %> </p>
<% end %>
The answer to the question is below. I had to use the .gsub and .html_safe methods.
<%= debug(params).gsub("controller:", "<u>controller:</u>").html_safe %>
This code keeps the existing html & css generated by rails intact

rails4 behavior based on link_to is nil

There is a website attr on product_lead table which is optional. If it's present then I wanna turn #produc_lead.lead into a link, but if it's not it should be plain text.
If I use the code below and the website is nil then the link points to the page the user is currently on. If I do it with #product_lead.try(:website), it's gonna be the same. But as I mentioned I would like to have plain text over link in this case.
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
After playing around I fell back to the following solution, but it's terrible. Any better ideas?
<% if #product_lead.website %>
<%= link_to #product_lead.website, target: "_blank" do %>
<%= #product_lead.lead %>
<% end %>
<% else %>
<%= #product_lead.lead %>
<% end %>
Maybe link_to_if if Rails 4
<%= link_to_if(#product_lead.website, #product_lead.lead, #product_lead.website) do %>
#product_lead.lead
<%= end %>
You can create custom view helper for this.
Well, link_to is going to generate a <a> tag, whether you provide a valid URL or not. So if the URL is nil, yes, it's gonna be a link for you own page.
If you want to "hide" this, you could call a partial in which you place you if/else and so on, but it's just to sweep this under the rug :)
Or if you wanna go further, as #Jovica Šuša, a view helper is the most elegant solution.

Basic Calculating Application

I am looking to learn a few things with Ruby on Rails and was wondering how I can make a basic calculator that doesn't touch the model in rails.
I am using form_tag
This is my main page
<%= form_tag do %>
<%= label_tag('first number') %>
<%= number_field('first_number', value = nil) %>
</br>
<%= label_tag('second number') %>
<%= number_field('second_number', value = nil) %>
</br>
<%= submit_tag("calculate") %>
<% end %>
<% first_number * second_number %>
I am getting an error that says :
undefined local variable or method first_number
How would I got about fixing this? I am not sure where to go from here?
Create a controller method. Then in the form_tag give the method's url. Then receive the parameters in that method. Then calculate and show the result.
Suppose,
Your current view calculator.html.erb show the form.
in the form tag use <form action="<%= calculate_result_path %>" method="post">
Here, calculate_result is a method in any controller.
def calculate_result
#result = params[:first_number] * params [:second_number]
end
Now make a view file for this method to show the result. Or you can ajaxify to show the result on the calculator.html.erb file.
To Ajaxify
suppose, you want to show the result in calculator.html.erb as like
<div class="show-result">
<%= #result %>
</div>
now create a calculate_result.js.erb for the method calculate_result. On that JS file replace the div with class show-result using a custom div where you put your result which you get from the controller method.
To learn more about, please learn how to ajaxify your views in rails. I'll recommend you to read the book "Agile Development"

Resources