Combine link_to and simple_format in rails - ruby-on-rails

in Rails I have to create an hyperlink using link_to.
I would like to introduce \n in the text of this hyperlink and to make it working I have to use the function simple_format. How can I concatenate the two? I tried only in the text but it doesn't work. If I put it around the whole link_to, I got an error.

You can pass a block of code to the link_to method like:
link_to(options = {}, html_options = {}) do
# name
end
Inside the block of code you can add the logic you need for the text of the link.
PD: please add your current code to the question

Related

parse form_for/form_tag to get the url or controller/action

I want to parse form_for and form_tag statements in erb files to determine which urls they post to or which controller/action will be called.
For example, given a ".html.erb" file, I want to get all "<%= form_for %>" tags out, somehow parse it, and get to know which exact controller/action pair will be called after I click to submit this form. For instance, the following file, https://github.com/jcs/lobsters/blob/master/app/views/stories/new.html.erb
line 7, "<%= form_for #story do |f| %>", can I determine which controller/action pair it will be mapped to by running some line of code? just like "routes.recognize_path 'form_for #story do |f|'" such kind of thing?
form_tag
form_tag(url_for_options = {}, options = {}, &block)
form_for
form_for(record, options = {}, &block)
Looks like I need to somehow get the url_for_options and options object out and get the url element out. Is there any easy way to do it or is there any existing tools that can achieve such functions? Does rails have any built-in functions for such thing?
i don't know if i understand your question correctly, especially the part about parsing your views etc, but you can generate urls by calling url_for.
for example in your rails console you can do the following:
include Rails.application.routes.url_helpers
default_url_options[:host] = "localhost"
url_for #user # => "http://localhost/users/1"

Setting dynamic link path with url parameters in rails

I'm building an app where I set links dynamically through a url parameter. I can't figure out how to make the link_to work with both a dynamic link and further url parameters.
TemplateController
def next
#template = Template.find(params[:t])
end
Next View
<%= link_to "#{#template.firstpage_link}(:t => #template.id, :prt => 1)" do %><%end%>
This is what it gives me:
http://localhost:3000/role/step2_path(:t%20=%3E%20#template.id,%20:prt%20=%3E%201)
I've tried a bunch of ways and I get either errors or this link
What you seem to be shooting for is something like
<%= link_to public_send(#template.firstpage_link, :t => #template.id, :prt => 1) do %>
public_send lets you call a public method by passing in its name as a symbol or string.
However, there may be more elegant ways to achieve this with the Rails router, as #Typpex is suggesting. If nothing else, you could clean up the view a bit with something like this in a helper:
def template_path(template)
public_send(template.firstpage_link, :t => template.id, :prt => 1)
end
And then calling that from your view.
I think you are not using link_to correctly, if you look at the link_to API
You will see that the first parameter is what you would like to be displayed and the second one is the rails path. You should pass your parameter when defining the rails path (or plain url) such as
link_to "display text", "#{#template.firstpage_link}?t=#{#template.id}&prt=1"
it would be better if you could use a rails route like
template_path(#template, prt: 1)

how do put %span inside rails link_to in haml

font awesome inside link_to helper with haml
how should I do?
i have this code
.link_header
{link_to_ledger(current_ledger) span.icon-flag }
help please
Just pass span as block to link_to helper method something like following:
.link_header
= link_to_ledger(current_ledger) do
span.icon-flag
There are two options for link in rails
As simple
= link_to 'my link text', controllers_path
As you want to do
.link_header
= link_to_ledger(current_ledger) do
%span.icon-flag
-# you can also do what ever you want here even table/images can also be added

Find all instances of a certain character and replace character & following word with hyperlink

I'm working on a Twitter replica project for a course I'm taking and am attempting to hyperlink any instance of #username in a tweet with a hyperlink to the appropriate username's profile page. Using gsub I can get the replacement to work, but if the tweet has multiple instances of different #usernames, it replaces them all with just the first username. Here's what I have so far:
def twet_link_replacer(twet)
if twet.content.include? "#"
username = twet.content.match(/#(\w+)/)
content_tag :p, twet.content.gsub!(/#(\w+)/, link_to(username, '/twets/'+username.to_s.gsub(/#/,""))).html_safe
else
content_tag :p, twet.content
end
end
Thanks!
You're doing way too much work here. There's no reason to call twet.content.include? "#" when you're using gsub, because gsub will just do nothing if # isn't found. You don't need the if...else, either, for the same reason. Something like this will suffice:
def twet_link_replacer(twet)
new_content = twet.content.gsub(/#(\w+)/) do |username|
link_to(username, "/twets/#{$1}")
end
content_tag :p, new_content
end
This uses a block argument to gsub, letting us replace matches with the result of link_to. Inside the block, username is the entire matched text (e.g. "#Jordan") and $1 is the first (and only) capture group (e.g. "Jordan").
There are a couple other issues with your code. First of all, do not use html_safe on user input. I'm assuming that twet.content comes from user input, and so is inherently unsafe. By trusting it (which is what html_safe implies—it tells Rails, "do not escape this string because I believe it is safe") you're making your app wide open to XSS attacks.
Second, when you're using string concatenation or interpolation (e.g. "/twets/" + username or "/twets/#{username}") to create a URL or path to give to link_to, you're probably making a mistake. It depends on what your routes look like, but if you're using resourceful routes, which you should, then e.g.
# instead of this...
link_to(username, "/users/" + username)
# you can just do this...
link_to(username, user_path(username))
...which will automatically generate a URL for you, and if you change your routes later on you won't have to change your views or helpers because user_path will change automatically along with the routes.
Again, this depends on how you've defined your routes, but it's the direction you should try to go.
The problem is you are using two different regex to match the username. Combine them to get what you want.
def twet_link_replacer(twet)
if twet.content.include? "#"
content_tag :p, twet.content.gsub!(/#(\w+)/, link_to('\1', '/twets/\1')).html_safe
else
content_tag :p, twet.content
end
end

How to display this span tag correctly in ruby?

The following the ruby code
content_tag(:li, render_menu_node(node, content_tag(:span, caption), url, selected))
displays the span tag like this:
<li><span>Foo</span></li>
How can I make it display like this?
<li><span>Foo</span></li>
If this is Rails 3, then use either
content_tag(:li, raw(render_menu_node(node, content_tag(:span, caption), url, selected)))
or
content_tag(:li, render_menu_node(node, content_tag(:span, caption), url, selected).html_safe)
or even better modify render_menu_node to return html_safe string. Be sure to h all user input inside that method however.
You may need to use one of these techniques inside your method. Also make sure you are not wrapping what you get from content_tag(:span, caption) in a h method call. Post your code for more details.
If this is not Rails 3, you'll need to post the source of that method.

Resources