I have a module in lib directory with the name "Transpo.rb":
module Transpo
class FT
def getCities
...
end
end
end
And in the controller I have
require 'Transpo.rb'
class TranspoController < ApplicationController
def index
#transpo = Transpo::FT.getCities()
respond_to do |format|
format.html # index.html.erb
format.json { render json: #transpo }
end
end
But when I run "http://localhost:3000/transpor" always gives the error:
NoMethodError in TranspoController#index
undefined method `getCities' for Transpo::FT:Class
Why? I've already set the auto_load lib in application.rb but continue with the same problem.
getCities is defined as an instance method, but you are calling it as a class method.
Either create an instance with something like instance = Transpo::FT.new, or change the definition of getCities to be def self.getCities to make it into a class method.
Related
I am trying to write a module and I want to replace that module with my action in my controller. For that I have created a module called test inside my controller folder which is. Where I want to put my action my controller action code is:
def test
rain_fall_type = "test"
year = ""
compare = params[:compare]
respond_to do |format|
format.html { render json: rain_fall_type }
end
end
I want to put this code inside my module code I have added this code into my module whose code is:
module Test
def test
rain_fall_type = "params[:rain_fall_type]
views = params[:views]"
year = ""
compare = params[:compare]
respond_to do |format|
format.html { render json: rain_fall_type }
end
end
end
And I am trying to extend this into my controller so I am putting extend Test into my controller but I am getting this error:
The action 'test' could not be found for ProductionProductivity7sController
When I remove def test from my module and put this code in controller like this:
def test
extend Test
end
And I remove def test from module and changed it to:
module Test
rain_fall_type = "params[:rain_fall_type]
views = params[:views]"
year = ""
compare = params[:compare]
respond_to do |format|
format.html { render json: rain_fall_type }
end
end
When I am doing this I am getting this error:
undefined local variable or method `params' for Test:Module
What should I do to just replace my test action into my module.
This is a good task for concerns, here is an example:
your controller code:
class ProductionProductivity7sController < ApplicationController
include Concerns::MyAwesomeModule
end
your module placed in app/controllers/concerns/my_awesome_module.rb:
module Concerns
module MyAwesomeModule
extend ActiveSupport::Concern
included do
# here you can use controller level methods
# like `before_action`
end
def the_action_name
# your logic goes here
# you can use all variables from original controller
# for e.g. `params`, `render` and so on.
end
end
end
Having resource Foobar with the following controller:
class FoobarController < ApplicationController
def new
#foobar = Foobar.new(baz: params[:baz])
#foobar.build_data
end
def create
#foobar = Foobar.new(foobar_params)
respond_with(#foobar)
end
# ...
end
Is it necessary to set instance variable #foobar in #create method? Could not I just write
def create
Foobar.new(foobar_params).tap &method(:respond_with)
end
?
It depends on what content types you respond with. The docs describe exactly what happens when you call respond_with. In your case, in the create action, respond_with is the same as the following, assuming you did not specify any other format than html in a respond_to call in your controller:
respond_to do |format|
if #foobar.save
flash[:notice] = 'Foobar was successfully created.'
format.html { redirect_to(#foobar) }
else
format.html { render action: "new" }
end
end
The only case where the #foobar instance variable would be necessary is, if there is a validation error and your new.html template includes #foobar. If the foobar_params are always valid, then respond_with will always respond with a redirect to the show action, so the instance variable is unnecessary.
I have a ActiveMailer class, and inside I am sending emails with attached PDF template using the render_to_string method like this:
def send_sales_order(email_service)
#email_service = email_service
#sales_order = SalesOrder.find_by_cid(email_service.order[:id])
mail(:subject => I18n.t("custom_mailer.sales_order.subject", company_name: "test"), :to => #email_service.recipients) do |format|
format.html
format.pdf do
attachments['purchase_order.pdf'] = WickedPdf.new.pdf_from_string(
render_to_string('sales_orders/show.pdf.erb', locals: {current_company: #sales_order.company})
)
end
end
end
Inside the show.pdf.erb template, I called my helper methods defined elsewhere such as the current_company method defined in ApplicationController like follow:
class ApplicationController < ActionController::Base
helper_method :current_company, :current_role
def current_company
return if current_user.nil?
if session[:current_company_id].blank?
session[:current_company_id] = current_user.companies.first.id.to_s
end
#current_company ||= Company.find(session[:current_company_id])
end
But these helper methods are not available when I use the render_to_string method to render the template, is there a way around this?
Thanks
ApplicationController.new.render_to_string works for me
Starting with Rails 5 you can use:
rendered_string = ApplicationController.render(
template: 'invoice/show',
assigns: { invoice: invoice }
)
This create a request-like object in a controller like env, assign a #invoice instance variable accessible in the template.
See documentation here for more options:
http://api.rubyonrails.org/classes/ActionController/Renderer.html#method-i-render
Just beat my head on the wall for a couple hours on this one. Finally found the add_template_helper method which did the trick.
class ApplicationController < ActionController::Base
add_template_helper ApplicationHelper
...
def foo
#foo = render_to_string(partial: 'some_file.rb')
end
end
This will make all methods from ApplicationHelper available, even when using render_to_string with a partial.
I have the code:
<h1><%= #app.name %></h1>
in show.html.erb and it runs fine. However when I paste that code to newsfeed.html.erb, it gives the following error message:
NoMethodError in Reviews#newsfeed
undefined method `url' for nil:NilClass
From reading other similar questions I think I need to make a change to reviews_controller.rb. This is (I think) the relevant code in that file:
def newsfeed
#reviews = Review.all
end
And the relevant code in the main AppsController
require 'Review'
class AppsController < ApplicationController
...
def show
#app = App.find_by_name(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #app }
end
end
I believe the solution is to add some code below def newsfeed but I'm not sure what to add. Or maybe that's not even the solution.
Thanks for any help!
#app is nil. In the show action you set it like so
#app = App.find_by_name(params[:id])
But you forgot to add it to the newsfeed action!
I want to write this library that responds to some json and html request. In the controller's action, I will call MyLib.search(params). Then in "module Something; class MyLib", I have "def search(params); respond_to ... render :json ...; end". If I try to use this library, I get "NoMethodError (undefined method `respond_to' ...".
How should I write this, so that I get respond_to and render in scope?
Perhaps a mixin would serve you better, something like this:
module Something
def search # params will be in scope so no need to pass it
#...
respond_to do |format|
format.json ...
end
end
end
and then in the controller:
class SomeController < ApplicationController
include Something
def whatever
# ...
search
end
end