How to get all models,controller,action and method names in Rails - ruby-on-rails

In runtime we can get the current controller and current action names by controller_name and action_name methods like wise,
I want to get all the remaining controllers and action names and models too if possible..
Any rails method available to get all the controller names and the action names in application controller.

#table_names = ActiveRecord::Base.connection.tables
#model_names = Array.new
#model_names.each do |table_to_model|
#model_names = #model_names.insert(#model_names.length,table_to_model.camelize.singularize) unless table_to_model.blank?
end
This is how you get all Model name

Related

Stuck in parsing URL and work with it in a view

Trying to parse a URL with this format http://landing.com?data=123 - I'm been able to get the Data through irb like:
require "addressable/uri"
uri = Addressable::URI.parse("http://landing.com?data=123")
uri.query_values['data']
=> '123'
But I'm stuck on how to interact with that 'data' within a Rails view. I have tried including it in Controller (pages_controller.rb in my sample) like:
class PagesController < InheritedResources::Base
def test
uri = Addressable::URI.parse("<%= request.original_url %>")
u = uri.query_values['data']
end
end
But no idea how can I extract that piece of data to be used within my Views. Any guidance on this?
If I open one of the views like where I call that 'test' method - I'm getting uninitialized constant PagesController::Addressable but made sure it's in my enviroment with gem which addressable/uri
Controllers have a lot of the query information already parsed. You can access it with params. In that case, you can use
u = params[:data]
As Sophie Déziel said, if it's under an app request, you can access to your query values through params hash. params is present in your controllers and views.
If you are talking about hardcoded URLs or URLS that you get from 3rd party sources, you will nee to create an instance variable in your controller (#u = ...) to be available in your views.
Note that you're not supposed to call action methods in your views, they are 'invoked' by Rails framework.
# controller
def my_action
# .....
#u = uri.query_values['data']
end
# view
<%= #u %>

Rails Check if Controller Index Exists

Is there a way to check if there exists an index action for a controller? Something like:
Controller.indexActionExists?
I have seen posts to check if specific routes exist, but those methods aren't working for me, since some of my index actions aren't associated with routes.
The action_methods method is going to be the most expedient tool in this case, returning a Set of method names. Give the following code a shot:
# Get a set of method names belonging to a controller called 'MyController'
methods = MyController.action_methods
if methods.include? "index"
puts "MyController has an index method"
else
puts "MyController lacks an index method"
end

Switch statement in one action or multiple actions in Rails controller?

Im making this site for clothes where I have different categories and I have a resource named "Items" for managing every clothing (since I've read somewhere, one controller per resource).
So for example, if I want to display jackets, I have a route like this:
get '/jackets', to: 'items#index', page: 'jackets'
And the controller has an index action that has a switch statement with all the different possibilities inside (I'm using scopes here):
def index
case params[:page]
# Women / Clothing
when "clothing"
#items = Item.clothing
when "beachwear"
#items = Item.beachwear
when "coats"
#items = Item.coats
Is this the right way do do it? Or should I make a single action for each kind of category that I have?
Thanks in advance and sorry for my bad english
Edit: I have something close to 100 categories.
Are Item.clothing, Item.coats, etc, scopes or? How have you implemented your model?
Why not just do something like:
def index
#items = Item.where(category: params[:category])
end
(provided, of course, that you add a category field in your Item model)
note that I changed params[:page] to params[:category] for semantic reasons

Indexing method that gets parameters in Rails 4

I have a store application with a Product scaffold and I want to enable categories and pages that show each category of products.
My product model has a "category" attribute and I use the link_to helper to create links to each category.
In my products controller I added a method called index_by_category(cat):
def index_by_category(cat)
#products_by_category = Product.where(category: cat)
end
I'm trying to iterate #products_by_category in a view I created with the corresponding name (product/index_by_category.html.erb) just like the regular index method do. For some reason it render me the regular index method of products which shows ALL of them, even though the URL is:
http://localhost:3000/products?index_by_category=Food
This is what I did in my route.rb file:
get 'products/index_by_category'
I'm newbie to Rails development so if I did something which is wrong from the roots and the rails approach to the problem should be entirely different I also be happy to know for the sake of learning.
You are doing things a bit wrong. Try to write your controller like this:
def index_by_category
#products_by_category = Product.where(category: params[:category])
end
And update your route
get 'products/category/:category', to: 'products#index_by_category
Then visit
http://localhost:3000/products/category/Food
UPDATE
if you really want to use index method for both cases you could do that by modifying it to something like this
def index
if params[:category]
#products = Product.where(category: params[:category])
else
#products = Product.all
end
end
and then just visit
http://localhost:3000/products?category=Food

Trying to make urls of the form <base_url>/boards/<name> in ruby on rails and getting errors

I am using ruby on rails to make a simple social networking site that includes different message boards for each committee of a student group. I want the url structure for each board to look like https://<base_url>/boards/<committee_name> and this will bring the user to the message board for that committee.
My routes.rb file looks like:
resources :committees, only: [:index]
match '/boards/:name', to: 'committees#index(name)'
My index function of committees_controller.rb file looks like:
def index(name)
#posts = Committee.where(name: name)
end
And then I'll use the #posts variable on the page to display all of the posts, but right now when I navigate to https://<base_url>/boards/<committee_name> I get an Unknown Action error, and it says The action 'index(name)' could not be found for CommitteesController.
Could someone guide me through what I have done wrong?
Once I get this working, how would I make a view that reflects this url structure?
Set up your routes like this:
resources :committees, only: [:index]
match '/boards/:name', to: 'committees#show'
and the controller like this:
def index
#committees = Committee.all
end
def show
#committee = Committee.find_by_name!(params[:name])
end
You can't really pass arguments to controller actions the way you were trying to with index(name). Instead, you use the params hash that Rails provides you. The :name part of the route declaration tells Rails to put whatever matches there into params[:name].
You also should be using separate actions for the listing of committees and displaying single committees. Going by Rails conventions, these should be the index and show actions, respectively.
When routing, you only specify the method name, not the arguments:
match '/boards/:name', to: 'committees#show'
Generally you will declare something with resources or match but not both. To stay REST-ful, this should be the show method. Index is a collection method, usually not taking any sort of record identifier.
Arguments always come in via the params structure:
def show
#posts = Committee.where(name: params[:name])
end
Controller methods that are exposed via routes do not take arguments. You may construct private methods that do take arguments for other purposes.

Resources