Rails undefined method in Controller - ruby-on-rails

I have a Link resource (Link as in url's). I have a method in my Links controller to determine whether or not a link that a user enters has the "http://" prefix, and if not, to append that prefix to the URL. Although I defined the method in my Links controller, I am getting an undefined method error.
Here is the relevant portion of my links controller:
helper_method :link_formatter
def link_formatter(url)
prefix = "http://"
url.include?(prefix)? url : prefix + url
end
Here is my links view:
<%= link_to link.description, link_formatter(link.url), :target => '_blank' %>
The error:
undefined method `link_formatter' for #<#<Class:0x007fbd51e2ea08>:0x007fbd5250cfa0>

The link_formatter is a generic function that can be used by any view in your Rails application. To make it accessible by all your views add it to the ApplicationHelper as #Damien Roche said. The application helper is located in app/helpers/application_helper.rb. You can also generate a new helper by running rails generate helper HELPER_NAME on the command line.

Related

undefined local variable or method when using custom scaffolded route

I am learning Ruby on Rails and was following a tutorial and scaffolded a CRUD app. I am trying to put link to the add a new entry in the navbar but it's giving the below error:
undefined local variable or method `phone_lists_new_path'
Here's the navbar code:
<%= link_to "List", phone_lists_path, class:"nav-item"%>
<%= link_to "Add contact", phone_lists_new_path, class:"nav-item"%>
phone_lists is a folder in View and new.html.erb is a file along with the below ones:
I guess I am doing something wrong in phone_lists_new_path, but it works in the other links like phone_lists_path and home_about_path
It was new_phone_list and not phone_lists_new, rake routes would show the route name.

Rails 4.2.5 Mailer URL issue

I have currently set up a mailer which is notifying users that their invoice is ready.
The mailer is working correctly except the URL is adding a . instead of a slash in the url. For example - I am receiving the following which isn't directing to the intended link:
www.mydomain.com/invoices.14
Instead I would like it to say:
www.mydomain.com/invoices/14
My Set up is as follows:
invoice_mailer.rb
default_url_options[:host] = 'mydomain.com'
invoice_mailer/invoice_ready.html.erb
<p>Click the following link <strong><%= link_to #invoice.inv_num, invoices_url(#invoice) %></strong></p>
I'm not sure why but the redirection keeps adding the . in the URL. Any help would be appreciated.
Use a singular version of url helper:
<p>Click the following link <strong><%= link_to #invoice.inv_num, invoice_url(#invoice) %></strong></p>
Here is an example how it's works for instance resources :invoices
invoices_path returns /invoices # that's why you have invoices.14
new_invoice_path returns /invoices/new
edit_invoice_path(:id) returns /invoices/:id/edit (for instance,
edit_invoice_path(10) returns /invoices/10/edit)
invoice_path(:id) returns /invoices/:id (for instance, invoice_path(10)
returns/invoices/10)
Each of these helpers has a corresponding _url helper (such as
invoices_url) which returns the same path prefixed with the current
host, port and path prefix.
Read the documentation CRUD, Verbs, and Actions

Getting an undefined method even though I have defined it in my controller

I’m using Rails 4.2.3. I want to have a tabbed menu on my admin login page, so I added the following to my app/controllers/admin_controller.rb file …
class AdminController < ApplicationController
before_filter :must_be_admin, only: :index
def index
#page_id = "index"
end
def menu_builder(page_id)
tabs = ['Current Hacks','Add New Hack','Approve Hacks']
content = ""
tabs.each do |tab|
content << if page_id == tab
content_tag('li', content_tag('a', tab, :href => nil ), :class => 'current') + " "
else
content_tag('li', content_tag('a', tab, :href => "/#{tab}" )) + " "
end
end
content
end
Here is what I have in my app/views/admin/index.html.erb file …
<h1>Admin#index</h1>
<ul>
<%= menu_builder(#page_id) %>
</ul>
However, when I visit my /admin/index page, I get this error
undefined method `menu_builder' for #<#<Class:0x007f861bfe9900>:0x007f861bff36f8>
Where am I going wrong?
for a rails method to be available inside your views,you must define it in helpers and not in controllers.
for example
admin_helper.rb
def menu_builder(page_id)
###definition
end
so now ,this method can be called within any views inside admins/ coming from admins_controller.rb
but if you want it to be available to all views,then you must add it in application_helper.rb
You cannot use controller actions right into your views (they are called from routes via HTTP requests).
You need the menu_builder method to be defined in an AdminHelper module, and then call helper AdminHelper in your AdminController.
See http://api.rubyonrails.org/classes/ActionController/Helpers.html for more information.
Hey it is looking this method in its helper . define that method in helper and then call it the view. You cannot call controller methods from a view..
Please add menu_builder method to AdminHelper(app/helpers/admin_helper.rb). Otherwise, add menu_builder method to some other helper and include that helper in your AdminController. It is preferable to have menu_builder method in AdminHelper.
Methods in a controller generally meant for "actions", typically have to be defined and consumed through Routes.
Just move the method from controller to any helper maybe applicaiton_helper then you are able to create view a you need

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.

How do I create a link (or button) that will execute an Application Helper method?

I am new to ruby/rails and trying to create links that will update a variable in session. I have the following method defined in application_helper.rb--I put it here in hopes that this is the correct place to have it so it can be used all all views:
module ApplicationHelper
def add_workspace_to_session(workspace_id)
session[:workspace_id] = workspace_id
end
end
In the view I want to create a simple link (or button) that will call this helper, passing in a workspace ID so that it updates session[:workspace_id]. How do I make this link in the view? Using link_to? I'm missing something...having to do with routes?
Thanks!
If you are creating a button or link it should be pointed to a url path not just a helper method. A quick fix for this is add a path to the helper method.
Note: I have not tested this with passing a parameter.
def add_workspace_to_session(workspace_id, url)
session[:workspace_id] = workspace_id
url
end
<%= button_to "Store Workspace", add_workspace_to_session(workspace_id, root_path), :method => :get %>

Resources