I'm writing a static page controller.
I get the menuname in the routes.rb and it's call the static controller show method.
match '/:menuname' => 'static#show'
And static_controller.rb:
#static=Staticpage.where("menuname =
?", params[:menuname])
But if I want print #static.title in the view, I get this error:
undefined method `title' for #
Whats wrong?
the SQL query looks good:
SELECT staticpages.* FROM staticpages WHERE (menuname = 'asd')
Couple of working alternatives:
#static = Staticpage.where("menuname = ?", params[:menuname]).first
#static = Staticpage.find_by_menuname(params[:menuname])
Related
I need a def in my application_helper.rb that will accept the model name passed as a variable.
I have a nav view where i am trying to access a particular record based on the content of $current_opportunity
<%# link_to "Lab", lab_path(get_id('Lab')), class: get_labs_class() %>
What I was trying to do was call get_id and pass 'Lab' to it.
My application_helper.rb has
def get_id(model)
model_ids = model.where("opportunity_id = ? ", $current_opportunity)
model_ids = model_ids.first
model_id = model_ids.id
model_id = model_id.to_i
return model_id
end
If I try to run like that, I get
NoMethodError in Prospects#show
Showing C:/Users/cmendla/RubymineProjects/product_development/app/views/shared/_tabs_nav.html.erb where line #61 raised:
undefined method `where' for "Lab":String
Trace of template inclusion: app/views/layouts/application.html.erb
Rails.root: C:/Users/cmendla/RubymineProjects/product_development
Application Trace | Framework Trace | Full Trace
app/helpers/application_helper.rb:25:in `get_id'
app/views/shared/_tabs_nav.html.erb:61:in `_app_views_shared__tabs_nav_html_erb__956794461_83328552'
app/views/layouts/application.html.erb:113:in `_app_views_layouts_application_html_erb___468328524_46368744'
However, if I change the def to
model_ids = Lab.where("opportunity_id = ? ", $current_opportunity)
i.e hard wiring Lab, everything works.
Is there any way to substitute the value of model in the where statement?
Try:
model_ids = model.constantize.where("opportunity_id = ? ", $current_opportunity)
constantize also works with symbols
I can access the shopping_list_products loaded in this API call from the console with no error.
I get a 500 on the API, however:
ActionView::Template::Error (undefined method `id' for #<Class:0x007f6cca7e1dd0>):
2015-01-26T23:51:07.608697+00:00 app[web.1]: 1: collection :#shopping_list_products
2015-01-26T23:51:07.608699+00:00 app[web.1]: 2: extends 'api/v1/shopping_list_products/_show'
index.json.rabl:
collection :#shopping_list_products
extends 'api/v1/shopping_list_products/_show'
show.json.rabl:
object :#shopping_list_product
extends 'api/v1/shopping_list_products/_show'
_show.json.rabl:
object :shopping_list_product
attribute(:id, :if => lambda { |m| m.id })
attributes :shopping_list_retailer_id, :product_id, ...
... more attributes
child :product, partial: 'api/v1/products/_show'
child :product_category, partial: 'api/v1/product_categories/_show'
node(:url) do |shopping_list_product|
api_v1_schedule_requisition_plan_shopping_list_shopping_list_retailer_shopping_list_product_path(#schedule, #shopping_list_retailer, shopping_list_product, format: :json)
end
EDIT: I removed the id attribute and then ran into the next error, "undefined method shopping_list_retailer_id for Class:". Why is this happening?
EDIT: Found out it's my code called from the controller.. if I return
#shopping_list_retailer.shopping_list_products
it works fine.
But I do this instead:
api :GET, '/schedule/:schedule_id/requisition_plan/shopping_list/shopping_list_retailers/:shopping_list_retailer_id/shopping_list_products', 'List all shopping list products for Shopping List for Requisition Plans for Schedule in the database'
param :query, String, desc: "Scoped_search style query string"
def index
#shopping_list_products = ShoppingListProductsIndexQuery.new(#shopping_list_retailer, params[:query]).shopping_list_products
end
class ShoppingListProductsIndexQuery
attr_reader :shopping_list_retailer, :query
def initialize(shopping_list_retailer, query)
#shopping_list_retailer = shopping_list_retailer
#query = query
end
def shopping_list_products
#shopping_list_retailer.shopping_list_products.ordered_by_product_category_type_and_product_category_name_and_product_name.search_for(query)
end
end
Still confused why undefined method id for Class is hit in the rabl view.
Your error message (undefined method 'id' for #<Class:0x007f6cca7e1dd0>) is saying the undefined method is on an instance of Class instead of an instance of ShoppingListProduct (or whatever your actual class name is).
This means the wrong thing is being rendered.
Probably because you are using:
object :#shopping_list_products
# and
object :#shopping_list_product
Instead of:
object #shopping_list_products
# and
object #shopping_list_product
Remove the :s.
Also, in your _show.json.rabl file, you really don't need
object :shopping_list_product
as this is ignored when the RABL template is being used as a partial (https://github.com/nesquena/rabl/wiki/Reusing-templates)
In rails 4.0.0.
Why does this work
#employees = Employee.where(:club_id => session[:club_id]).page(params[:page])
but not this?
#payments = Payment.where(:club_id => session[:club_id],
:trading_date => trading_date).page(params[:page])
On the second form I get an array error. I know how to fix it but I am perplexed as to why this occurs?
my error is this
NoMethodError (undefined method `page' for #<Array:0x007ff72845b380>):
app/controllers/payments_controller.rb:30:in `index'
Are you sure you dont want to do
#payments = Payment.where(:club_id => session[:club_id])
.where(:trading_date => trading_date)
.page(params[:page])
I am trying to return all records that match a search in Kaminari and paginate the results. However, I am getting the following error:
undefined method 'page'
my controller code:
#search = Sunspot.search(Building) do
fulltext params[:search]
end
#buildings = #search.results.page(params[:page]).per(15)
I think I am just not understanding how to use Kaminari?
page is a method you can call in relation, you can do this:
#buildings = Building.where(id: #search.results.map(&:id)).page(params[:page]).per(5)
A company model has many tag and has a country_id field. I would like to find:
all companies, located in a certain county
all companies, located in a certain county and has a certain tag if params[:tag] is present.
The first query is pretty easy
Company.where(:country_id => params[:country_id])
As for second one, I tried some queries and nothing worked
companies = Company.where(:country_id => params[:country_id])
companies = Company.tags.where(:name=> params[:tag])
undefined method `tags' for #<Class:0x000000055dfb60>
If I put
Company.tags.where(:name=> params[:tag])
then the error is the same
undefined method `tags' for #<Class:0x000000055dfb60>
In Rails console a command Company.first.tags receives all tags as it does.
UPDATE: this works
Company.joins(:tags).where("tags.name = ?", query_hash[:tag])
But I don't understand yet how to do something like this
my_conditions = get_search_conditions
if query_hash[:tag].present?
companies = Company.all(:conditions => my_conditions).joins(:tags).where("tags.name = ?", query_hash[:tag])
else
companies = Company.all(:conditions => conditions)
end
The error is
undefined method `all' for #<Array:0x007fbec8063e00>
The error is undefined method all for #<Array:0x007fbec8063e00>
It should work if you replace Company.all with Company.where
check if this works - Company.joins(:tags).where("tags.name",params[:tag])