Making a instance variable a clickable direct link - ruby-on-rails

In my app a user can create a post that has a link to a given website.
Example..
post.title -> I use this site to research topics.
post.link -> google.com
In post.show I would like to have a direct link to google.com
I try to do it simply with this:
<p><%= link_to #post.link%></p>
I need the link to route the user to google.com. However, it routes the user to
/topics/12/posts/www.google.com

That's because you're missing http:// on the Post's link attribute. When saving the record, you could check that the link string starts with http:// or https:// and if it doesn't, prepend the string accordingly.
Update:
Add a callback to your Post model:
before_save :prepend_link
Add a private method to your Post model:
private
def prepend_link
self.link = "http://#{link}" unless link.starts_with?('http://', 'https://')
end
Use this in your view:
<%= link_to #post.link, #post.link %>

Related

Missing character from link Ruby on Rails

I have a Link resource. In my view, I have a series of links that people can click to open another web page:
<%= link_to link.description, "http://#{link.url}", :target => '_blank' %>
If the value of #{link.url} is www.google.com, the link works fine. However, if the value of #{link.url} is http://www.google.com and I click it, it will go to the address http//www.google.com <--- notice it's missing a : after http. Can someone help me remedy this?
Thanks!
How about you add a helper method that checks to see if link.url contains 'http://' or not.
You could use something like this:
def link_formatter(url)
prefix = "http://"
url.include?(prefix)? url : prefix + url
end
That will check to see if link.url contains "http://", if it doesn't it will return the proper format for your url, if it does, it will just return link.url as is.

Rails How to Generate a Google Drive-like permalink?

When we share a Google Drive Form, it will give our a public url.
How can we implement this in our Rails application? It should be random and not repeated.
Could anyone help me? Thanks.
Update
I mean like this url :
https://docs.google.com/forms/d/1PPVIMrDo61Er9tqYlJRntfNT73jpxtd_YJGGjXOMlAw/edit?usp=drive_web
But I want a url like this form:
http://yourhost.com/1PPVIMrDo61Er9tqYlJRntfNT73jpxtd_YJGGjXOMlAw
You should add a permalink field to the model whose show action URL you want to share. Really you could use just /model/:id but if you want to use /model/:permalink then just add the new field, generate the permalink with something like SecureRandom and save it to the model, then build the URL and share it.
You could do something like this:
class SomeModel < ActiveRecord::Base
after_create :generate_permalink
private
def generate_permalink
self.permalink = SecureRandom.urlsafe_base64(32)
end
end
Then in some view where your user can find the permalink url:
<%= link_to "Title of the model", some_model_url(some_model.permalink) %>
The above helper would create a URL that goes to the show action of your some_model controller. You could, of course, create a new action if you wanted and add it to your routes, but I'm just going the simpler way.
In your controller's show action you'd need to find the model by its permalink:
class SomeModelController < ApplicationController
def show
#some_model = SomeModel.where("id = :id OR permalink = :id", id: params[:id]).first
end
end
With a bit more tweaking in your routes and view, you can shorten the URL to what you posted in your question:
http://yourhost.com/1PPVIMrDo61Er9tqYlJRntfNT73jpxtd_YJGGjXOMlAw
For this to work, you'd have to add a route to the bottom of your routes file so that when no other route is matched, your permalink route will catch the random string and dispatch it to the controller of your choice:
# config/routes.rb
get "/:permalink", to: "some_model#show", as: :permalink
Here the param will be called params[:permalink] rather than params[:id] in your controller. You could simplify the code in your controller by making the route get "/:id" but I think it's good to be explicit.
Then, just change your view to output the correct URL:
<%= link_to "Title of the model", permalink_url(some_model.permalink) %>
Hope that helps.

Link back to page visited before form

I have a listing page, then a form, then a thank you page. I need to put a link on the thank you page that takes the user back to the page they were on before the form which always varies. I've tried using this:
= link_to "Back", :back
But this only takes them back to the previous page, so the form.
Try this
<%= link_to 'Back', url_for(:back) %>
# if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# => http://www.example.com
here is more details.
Well, you can set a method in the form page to collect that url. The basic idea is to use a custom session variable to store previous url and keep it to next session.
Suppose your form's action is SomeController#new, then
class SomeController < ApplicationController
after_action "save_my_previous_url", only: [:new]
def save_my_previous_url
# session[:previous_url] is a Rails built-in variable to save last url.
session[:my_previous_url] = URI(request.referer || '').path
end
end
Then in the thank you page, you can get this my_previous_url by
session[:my_previous_url]
This should be able to suit your case, the previous url two pages ago.
Disclaimer: This is not verified. Idea only.
Add
Session belongs to controller. It is not a helper you can use directly in view. You need to define an instance variable in controller and then you can use it in view. Like this
# Controller
#back_url = session[:my_previous_url]
# View
<%= link_to "Back", #back_url %>
You can use the example from Rails API:
<%= link_to "Back", :back %>
Rails API Doc for link_to
Using a :back Symbol instead of an options hash will generate a link to the referrer (a JavaScript back link will be used in place of a referrer if none exists).
Since you saying,it might be different page before form, probably request_url can help you. you can save your request_url in a param and redirect to param_url if there is.
here is a source that you can take for reference.
http://programming-tut.blogspot.com/2010/06/ruby-on-rails-request-url.html
If use in Controller, you can direct use like this:
def some_action
# some code
redirect_to :back
end
This works for me:
In controller from previous view:
cookies[:original_referrer] = request.orignal_url
to set a cookie on the browser with the URL of the originating page
In the controller from the current view:
redirect_to cookies[:original_referrer]

How can I have the user only add their #name instead of entire Twitter link in a url_field?

In my Rails app I have a user model (Devise) with url_field for Twitter.
Right now I have this in my show page:
<%= link_to #deal.user.company_twitter, #deal.user.company_twitter, :class => "comp_twitter" %>
It shows the entire link in the show page (even http://), that's because my url_field makes the user add "http://" or it won't validate the link.
I want the user to only add their "#name" instead of the whole Twitter link when creating their profile. I would also want to show only the #name instead of the Twitter link in the "show" page. How can I do this?
Simply don't use an url_field, but a regular string database field, like twitter_username. When signing up, let the user enter their username, e.g. foo.
For getting the real URL to the Twitter account, create a new method in your user.rb model:
def twitter_url
"http://twitter.com/#{self.twitter_username}"
end
The advantage is that at this point, in your model, you can include a custom validation that would check if this URL really exists after the user has submitted their twitter_username.
Finally, use that in your view instead of just the URL:
<%= link_to "##{#user.twitter_username}", #user.twitter_url %>
This would render:
#foo

Helper method for url?

I have multiple fields for social networking websites where users can enter their username and it would link to their twitter, facebook, myspace account etc. My problem is how can I have it so when a user enters their username I can link to their account.
Here's what I have and I know this is probably not correct. In my profile index.html
<%= link_to twitter_url %>
profile helper method
def twitter_url
return(self.url= "http://twitter.com/#{profile.twitter}/account")
end
In my profile controller
include ProfilesHelper
The actual field in database is called
twitter_link
How can I get the just the user username then on my part which will do the rest link to their twitter, facebook etc account page?
How is that method supposed to know what the profile object is without you passing it in?
I'm imagining there being a #profile variable defined in your controller. In the view, you would have this:
<%= link_to twitter_url %>
And in the helper, this:
def twitter_url
"http://twitter.com/#{#profile.twitter_link}/account"
end
Note here that you don't need to set self.url or have an explicit return. This is because the method will automatically return a String which is what link_to takes.
If you want this method to be in a helper that's available in just your ProfileController, then why not put it inside ProfileHelper so that it's automatically included just by being related by name?

Resources