Ruby on Rails get URL and use it within an if statement - ruby-on-rails

I have a ruby on rails application, and want to display an image on the index page depending on what the URL is. If the url is: localhost:3000/products/multi_find I do not want to show the image, if it is anything else I do want it to be shown.
Is their a way I can retrieve the url and store it in a variable to run an <& if statement like: if url != localhost:3000/products/multi_find &><%= image tag "test.png", :size => "15x18" %> <% end %>
Thank you in advance.

None of those solutions worked, this did:
<% if request.original_url == 'http://localhost:3000/products/multi_find' %>
<%= image tag "test.png", :size => "15x18" %>
<% end %>

In Rails you must think in terms of routing rules, not URLs. An URL is nothing else than the result of a routing directive.
Therefore, /products/multi_find can be expressed in terms of action, controller and parameters.
With that in mind, you can use the current_page? helper to check if the current URL matches the route you expect, if it does then ignore the image.

You can even use rails "action_name" and "controller_name" helper methods to get the job done.

Try this:
<% unless current_page?('/products/multi_find') %>
<%= image tag "test.png", :size => "15x18" %>
<% end %>

Related

Best way to fix undefined method

I am building a rails app where I have a museums page which has a feature where it displays the museum with the most exhibits. The problem is that when there are no exhibits added to the db it gives an undefined method 'museum_name'. So the problem I have is I am not sure what would be the best way to make a check that would still allow me to access the page if there are zero exhibits?
Museums controller:
def index
#museums = Museum.all
most_exhibits = Exhibit.most_exhibits
most_exhibits.each do |museum|
#top_museum = MuseumsHelper.get_museum_name(museum.museum_id)[0]
end
Helper class method being used:
def self.get_museum_name(museum_id)
Museum.where(id: museum_id)
end
Display in views:
<%= #top_museum.museum_name %>
The best way to do it depends on how you want it to be. I think the ideal solution for yours is to check if/else then show the content accordingly:
<% if #top_museum.present? %>
<%= #top_museum.museum_name %>
<% else %>
<span>Nothing to display</span>
<% end %>
Or using try <%= #top_museum.try(:museum_name) %> or if you have ruby 2.3.0 or newer you can use safe navigation operator <%= #top_museum&.museum_name %> (Read more).
You could use try in your helper, that way it tries the query, if it fails then it returns nil
def self.get_museum_name(museum_id)
Museum.try(where(id: museum_id))
end
Ref: https://apidock.com/rails/v3.2.1/Object/try

Ruby On Rails image_path

link_to(image_tag("icons/#{icon_name}.png"),url_or_object,options) I was trying use it like that but when I enter the project I'm seeing it like this http://prntscr.com/329yzz I can't see the image please help me,I am beginner in Ruby too
Please Help me
You are getting raw html output.
Use html_safe as shown below:
EDIT
As per OP's comments, html_safe was required in project_title_links method to convert the link returned from link_to_icon into an HTML safe string(DOM ready):
def link_to_icon(icon_name, url_or_object,options={})
link_to(image_tag("icons/#{icon_name}.png", ),url_or_object,options)
end
def project_title_links(project)
content_tag :h1 do [project.title, link_to_icon('show',project) ].join(' ').html_safe end
end
link_to takes an optional block, if you need anything more complicated than text:
link_to url_or_object, options do
image_tag("icons/#{icon_name}.png")
end
Since those method is being used within the views, link_to is using capture to evaluate the block. Hence it can be used like:
<%= link_to url_or_object, options do %>
<div class='wrapper'>
<label>Some text</label>
<%= image_tag("icons/#{icon_name}.png") %>
</div>
<% end %>

Rails, link_to helper with an URL as a param

I want to generate the next html link:
http://url.com
To reproduce it using the link_to helper I have to write:
<%= link_to "http://url.com", "http://url.com" %>
What doesn't look DRY at all, I was expecting this to work:
<%= link_to "http://url.com" %>
But the above code generate a link targeting the actual request.url, not the one I'm sending in the param.
Am I missing something?
You're not missing anything --- the normal case is for the URL and the text that shows to the user to be different.
If you'd like, you could create a helper like
def link_to_href(link, args={})
link_to link, link, args
end
then, when you use it,
<%= link_to_href "http://url.com" %>
Will output
http://url.com
If you take a look at the source code of link_to you will see that at line 248 the a tag label is build with name || url.
That's why you have this behaviour and there is noway to do it like you're expecting.

Specifying url in rails 3

i am totally new to rails programming... i used the request.path to get the current url and display it in all my views by specifying it in applications.html.erb. It is returning the entire path and i want to display it as a link... so i use
link_to to specify it as url..now here is what i want to do.. the url returned will be in the format path1/path2/path3..... i want to display it as path1>path2>path3 and as a link such that when the user clicks path1, it should take him to path 1 and so on...
this is the code i gave in html.erb file
but i get an error that says undefined method.... what should i do to accomplish that??
You could split the request.path on / and then build up the various links, but that could get really cluttered for deeply nested paths. I think a better approach would be to use something like breadcrumbs_on_rails and declare your breadcrumbs explicitly and render them in a partial or helper method. I think you could also use some Rails filter magic to have action names breadcrumbed automatically, but making the breadcrumbs explicit forces you to think about your site and your users more than programmatically vomiting out a string of links of unknown length.
You can do like this:
<% path = request.path %>
<% links = path.split('/') %>
<% ll="/" %>
<% links.each do |l| %>
<% ll += (l+'/') %>
<%= link_to l,ll %> >
<% end %>

What is wrong in this ruby statement?

I am trying to do something when I am connecting to my own server(local).
I found request.env from the website, so I am using that array to compare my IPs.
<%
if request.env['HTTP_HOST']!="127.0.0.1"
puts request.env['HTTP_HOST']
else
puts "its Local!"
end
%>
When I run above in rails3, I get nothing printed...
I am new to ruby&rails3..
When you want output in the web page, use <%= %>, not <% %>. The output will be the return value of the expression, so you don't want puts.
<%=
if request.env['HTTP_HOST']!="127.0.0.1"
request.env['HTTP_HOST']
else
"its Local!"
end
%>
Note that you can also use the local? method instead of checking environment directly.
<%=
if request.local?
"its Local!"
else
request.env['HTTP_HOST']
end
%>
If you like conciseness you can do it as one line:
<%= if request.local? then "its Local!" else request.env['HTTP_POST'] end %>
For even more view conciseness, make use of a helper method:
<%= ip_or_local %>
where in the matching view helper you put:
def ip_or_local
if request.local?
"its Local!"
else
request.env['HTTP_HOST']
end
end
For this simple case, it may be overkill but in general when you start seeing lots of code in your view, it's time to think about hiding certain things in helpers.
puts will write to the server in this case, not to the response. so you should look for your message in the log of the server.
Do you want to print to the log or to the view?
It might be clearer if you break things up into seperate erb tags.
<% if local? %>
<%= "Text for local" %>
<% else %>
<%= "Text for remote" %>
<% end -%>
You need to use <%= %> tags for lines you want printed and <% %> tags for lines that you want logic in, like conditionals.
If you're new to rails, you should check out the peepcode rails from scratch videos, they're pretty cheap and a lot of rails developers built their base on what's in them. Railscasts are also snack sized little tutorials that will easily get you through a lot of the basics.
Recommended reading:
RUBY:
The ruby pickaxe
The ruby way
Rails:
The Rails way
Head first ruby on rails
Hope I was of some help.

Resources