Creating full_url method in Rails - ruby-on-rails

I'm trying to make a method to check if the url is an absolute or relative url. I'm calling the method from my view template, but it's raising undefined method error. I believe it's not calling the method, and that's why raising the error, but I'm not sure how to make it work now.
Anything helps, thank you.
UsersController
def url_checker(url)
/^http/i.match(url) ? url : "http://#{url}"
end
users/show.html.slim
span= link_to_if #profile.company_url.present?, #profile.company_url, url_checker(#profile.company_url), target: "_blank"
Error message

Related

Rails - How To Get Full URL in Model

In my Rails 5 app, I'm trying to get the full URL for my instance, but when I tried to use url_for as suggested in this SO question, I get an error, NoMethodError: undefined method 'url_for' for #<Product:0x8646fc0>.
Full URL with url_for in Rails
def get_url
url = url_for(self)
end
I also tried product_url(self) and received a similar error.
What's the proper way to get the URL for my instance? Thanks!
helper method is only available in view or decorator.
it's better to define url in the decorator.
if you want to use helper method in the model, try this
def url
Rails.application.routes.url_helpers.product_url(self)
end

How to call a helper method in view

I have this code in my helper:
def app_state(app)
state = app.operator_apps.map(&:is_production)
if state.include?(true) and state.include?(false)
"Sandbox/Production"
elsif state.include?(true)
"Production"
else
"Sandbox"
end
end
and in my view I have done this:
<%= app.app_state %>
I get the following error:
ActionView::Template::Error (undefined method `app_state' for #):
Please help me out in solving this.
The error you are getting will persist unless an app_state method is defined somewhere within app's model.
Try calling the helper method like so:
<%= app_state(app) %>
Now the app object is being passed in as the argument of the app_state method, instead of the method being called on the object itself.
Hope that helps!

This may be very simple but how come this code <%= #user.name %> doesn't work on all pages?

I get the following error after putting it on certain pages in Ruby on Rails:
undefined method `name' for nil:NilClass
It also says there is no method error in the page I am on. I know this may be a beginner question but it has been stumping me big time in my project. If you can give me some advice I'd be very happy! Thank you in advanced!
Fist of all: It's a common trap. We all fall into it from time to time.
When ever you read undefined method foobar for nil:NilClass you know that something is nil what shouldn't be nil. So you have to check why #user is nil. Maybe a bug in the controller.
If you want to handle both situations (nil AND User) than you can use this code:
<%= #user.name if #user %>

Undefined AWS::S3 when define a method in helper

I am following this tutorial http://net.tutsplus.com/tutorials/create-a-simple-music-streaming-app-with-ruby-on-rails/, but use the aws_sdk instead of aws_s3. I see basically they do the same thing. In the download part, I put the the download function into the model and it did show correctly the url to download, but from there I don't know how to trigger download so I moved the function to the helper and invoke it straight from view. From there rails keep complaining about undefined method `model_name' for URI::HTTPS:Class
This is the download method
def download song_key
bucket = AWS::S3.new.buckets['mybucket'] # error from this line because undefined AWS::S3
song = bucket.objects[song_key]
song.url_for(:read, expires: 10*60)
end
This is the views
<% #songs.each do |song| %>
<%= link_to "download", download(song.key) %>
<% end %>
Any idea how to fix it ? Thanks
You're reading the stack trace slighty wrong - it's not your helper method raising the exception, but something inside link_to.
The url_for method is returning a URI::HTTPS instance. When the second argument to link_to is something other than a string, it assumes that it's an activemodel class and tries to find the appropriate route from that. For example if you do
link_to 'Show', person
and person is an instance of Person, link_to will end up generating the url from person_path(person).
URIs aren't active model, so this process of finding the appropriate route fails. All you need to do is turn the URI into a string, for example
def download_url song_key
bucket = AWS::S3.new.buckets['mybucket'] # error from this line because undefined AWS::S3
song = bucket.objects[song_key]
song.url_for(:read, expires: 10*60).to_s
end
Apparently the equivalent method in aws_s3 returning strings rather than URI objects which is qhy the tutorial you are following doesn't do this.

My Rails app doesn't like my attributes

I am new to Rails and Ruby and actually it's my first web language. This isn't my first project but it's the bigest so far and I am having trouble with a certain page (the "home" page).
The error is:
NoMethodError in Home#index undefined method `avatar' for nil:NilClass
Extracted source (around line #1):
1: <%= image_tag current_user.avatar.url(:thumb) %>
2: <ul>
3: <li><b>Name:</b><%= current_user.name %></li>
4: <li><b>Username:</b><%= current_user.username %></li>
So it doesn't like my attributes :( What should I do in this situation? Where should I look first? Is this a controller problem?
The problem is that current_user is returning nil.
Take a look at the error
NoMethodError in Home#index undefined method `avatar' for nil:NilClass
Ruby is saying that you are trying to execute an instance method avatar on the nil object. After looking at your code, you should be able to tell that you are calling avatar on the return value of current_user so you can deduce that current_user is returning nil instead of the user.
Per the comments:
To solve this you will need to first determine if there is a current_user. Then and only then do you try to display a users avatar or the users other attributes (name, username ...)
Some people do not like logic in the views, I tend to follow this practice.
To avoid this, you could: create a view helper that contains the logic. This helper may or may not display/return data about a user.
Something along the lines of if current_user ...
You are trying to get the avatar for the 'current_user'. The system is complaining because it doesn't know who or what current_user is.
Your current_user object is nil. The code causing the problem (probably form your application or sessions helper) where you are setting the current_user...is most likely where you'll find the root of the problem.

Resources