I'm looking for a way to use something similar to link_to_unless_current but for sub URLs, so for instance for both these URLs:
/entity_name/
/entity_name/123
it will not result as a link. How can I achieve that in Rails?
UPD: Resolved it my own custom way, please Ruby savvy let me know if it's good.
module ApplicationHelper
def link_to_unless_current_or_sub(name, options = {}, html_options = {}, &block)
link_to_unless current_page_or_sub?(options), name, options, html_options, &block
end
private
def current_page_or_sub?(options)
url_string = CGI.unescapeHTML(url_for(options))
request = #controller.request
if url_string.index("?")
request_uri = request.request_uri
else
request_uri = request.request_uri.split('?').first
end
request_uri.starts_with?(url_string)
end
end
Usage:
<%= link_to_unless_current_or_sub("Users", users_path) %>
i don't think such helper method exist
you have to write something like following
<% if params[:id] %>
"Home"
<% else %>
<%= link_to "Home", { :action => "index" }) %>
<% end %>
Works in Rails 3.2 with some minor adjustments
def current_page_or_sub?(options)
url_string = CGI.unescapeHTML(url_for(options))
if url_string.index("?")
request_url = request.fullpath
else
request_url = request.fullpath.split('?').first
end
request_url.starts_with?(url_string)
end
Related
new.html.erb
<%= form_for #star do |f|%>
<%= star_radio(f, true)%>
<% end %>
stars_helper.rb
module StarHelper
def star_radio(form, status)
form.label "star_#{status}", class: 'radio-inline' do
form.radio_button(:star, status) + i18n_star(status)
end
end
def i18n_star (status)
I18n.t("activerecord.attributes.star.is_sun.#{status}")
end
end
I saw a piece of code like above.
I am not familiar with custom form helper.
Could you let me know why we can use form.radio_button(:star, status) + i18n_star(status) inside a block and why we can use '+' to add text on radio buttons.
I will be appreciated if you could tell me where I can go to learn this.
form.radio_button helper returns a string and I18n.t too returns a string. So, you can concatenate them.
More details how form tag is generated
This is a code of radio_button:
https://github.com/casunlight/rails/blob/master/actionview/lib/action_view/helpers/form_helper.rb
def radio_button(object_name, method, tag_value, options = {})
Tags::RadioButton.new(object_name, method, self, tag_value, options).render
end
Look at implementation of render method
https://github.com/casunlight/rails/blob/master/actionview/lib/action_view/helpers/tags/radio_button.rb#L20
def render
options = #options.stringify_keys
options["type"] = "radio"
options["value"] = #tag_value
options["checked"] = "checked" if input_checked?(object, options)
add_default_name_and_id_for_value(#tag_value, options)
tag("input", options)
end
Tag helper generate html tag and return his as html safed string:
https://github.com/casunlight/rails/blob/master/actionview/lib/action_view/helpers/tag_helper.rb#L67
def tag(name, options = nil, open = false, escape = true)
"<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}".html_safe
end
I need a helper that generates a link wrapped in a <li> including an active class.
Without supporting blocks this is easy:
def nav_item(*args, &block)
url = args[1]
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
link_to(*args)
end
end
But like link_to I want my helper to support blocks for defining content. With link_to I can do:
So how do I support the same in my helper?
All I need to do is pass the block through to link_to. My current attempt
def nav_item(*args, &block)
url = if block_given?
args.first
else
args[1]
end
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
if block_given?
# What goes here?
else
link_to(*args)
end
end
end
You can just pass the block to link_to as the last arg. Like this:
def nav_item(*args, &block)
url = if block_given?
args.first
else
args[1]
end
clazz = 'active' if current_page?(url)
content_tag(:li, :class => clazz) do
if block
link_to(*args, &block)
else
link_to(*args)
end
end
end
So im creating a website, and on that website people can post links. Depending on what the link is, the post will render differently. For example, a post with a link to youtube.com/somevideo will render an embeded video. A link ending in JPG will display that image.
I have written the following in my view
<% link_type = extract_content_from_url(post.link) %>
<div class ="preview_of_post">
<% if post.link %>
<% if link_type == "youtube" %>
<%= youtube_embed(post.link) %>
<% end %>
<br />
<%= link_to (post.link), (post.link) %>
<% end %>
And in my helper, I have this:
module PostsHelper
def extract_content_from_url(url)
if url != ""
unless url.include?("http://")
post.link = "http://#{post.link.downcase}"
end
else
return "nil"
end
if url.include? == "youtube.com/watch" #youtube link
return "youtube"
end
if File.extname(url) == ".gif" || File.extname(url) == ".jpg" ||
File.extname(url) == ".png" || File.extname(url) == ".jpeg"
return"picture"
end
end
def youtube_embed(youtube_url)
if youtube_url[/youtu\.be\/([^\?]*)/]
youtube_id = $1
else
youtube_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
youtube_id = $5
end
render(:inline=> "<iframe title='YouTube video player' width='640' height='390' src='http://www.youtube.com/embed/#{ youtube_id }' frameborder='0' allowfullscreen></iframe>")
end
end
When running my code, I get the following error:
Any suggestions?
Thanks.
url.include? == "youtube.com/watch" is not valid. include? is a method which takes an argument. You probably mean url.include? "youtube.com/watch".
I am completely new to Rails and I'm trying to make each filetype that is in a directory have its own icon, i can only get it to show one please help? Here is what I have so far.
Controller:
class DocsController < ApplicationController
def port
#files = Dir.glob("public/folder/*")
filetype = [".pdf", ".txt"]
if filetype.include? ".pdf"
#extension = "pdf.png"
elsif filetype.include? ".txt"
#extension = "text.png"
else
#extension = "folder.png"
end
end
end
View:
<% #files.each do |file| %>
<div class="filediv">
<%= image_tag #extension, :size => "150x150" %>
<p><%= file.gsub("public/folder/", "") %></p>
</div>
<% end %>
This is resulting in everything having the pdf icon, can someone tell me what i am doing wrong?
Thanks
I think you need to add helper method
def extension_image(file)
ext =File.extname(file)
if ext==".pdf"
"pdf.png"
elsif ext == ".txt"
"text.png"
else
"folder.png"
end
end
<%= image_tag extension_image(file), :size => "150x150" %>
filetype = [".pdf", ".txt"]
remove this code.
if filetype.include? ".pdf"
#extension = "pdf.png"
elsif filetype.include? ".txt"
#extension = "text.png"
else
#extension = "folder.png"
end
end
Now why is always displays a pdf extension
it's simple
[1,2,3].include?(1) it's always true so no further checking
Checkout how include works in array.
Here below the start of my menu_builder method in the applicationhelper (in the view: <%= menu_builder(#page); %>):
def menu_builder(page)
items = [ "home", "faq", "store" ]
content = ""
items.each do |i|
content << content_tag(:a, "#{i.capitalize}", :href => "/#{i}" )
end
return content
end
I would like to render links not tags. I should miss somthing here but I don't find..
Thanks for your help!
You can fix it like this:
def menu_builder(page)
items = [ "home", "faq", "store" ]
content = ""
items.each do |i|
content << content_tag(:a, "#{i.capitalize}", :href => "/#{i}" )
end
content.html_safe
end
Or modify your template like this:
<%= raw menu_builder %>
Use any.