Rails use a definition for two actions? - ruby-on-rails

Is there a way to use the same definition for 2 actions?
Something like this?
def index2, index5
#workgroups = Workgroup.all
if params[:workgroup]
#workgroup = Workgroup.find(params[:workgroup])
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #workgroups }
end
end

You can route the 2 urls to the same action.
On config/routes.rb do:
get 'controller/index5', to: 'controller#oneindex'
get 'controller/index2', to: 'controller#oneindex'

Three solutions
1 Make Common method
I would prefer this approach because in case if you want to change something...
def index5
common_method_name
end
def index2
common_method_name
end
private
def common_method_name
# common code
end
2 Alias
def index5
# your code
end
alias_method :index2, :index5
3 routes
You can route the 2 urls to the same action in config/routes.rb.(source Murifox's answer)
get 'controller/index5', to: 'controller#oneindex'
get 'controller/index2', to: 'controller#oneindex

Related

Extending Rails Engine controller method without duplicating it

How to extend a controller method from a Rails Engine without having to duplicate the whole thing?
Trying to extend https://github.com/radar/forem/blob/rails4/app/controllers/forem/forums_controller.rb -- app/decorators/controllers/forem/forums_controller_decorator.rb:
Ideal
Forem::ForumsController.class_eval do
def show
# A simple `include` here or something?
# New code goes here...
end
end
Current
Forem::ForumsController.class_eval do
def show
# Repeat ALL the code from:
# https://github.com/radar/forem/blob/rails4/app/controllers/forem/forums_controller.rb
authorize! :show, #forum
register_view
#topics = if forem_admin_or_moderator?(#forum)
#forum.topics
else
#forum.topics.visible.approved_or_pending_review_for(forem_user)
end
#topics = #topics.by_pinned_or_most_recent_post
# Kaminari allows to configure the method and param used
#topics = #topics.send(pagination_method, params[pagination_param]).per(Forem.per_page)
respond_to do |format|
format.html
format.atom { render :layout => false }
end
# New code goes here...
end
end
We use this gem for multiple applications and engines to do exactly what you want:
https://github.com/EPI-USE-Labs/activesupport-decorators
I could extend a controller method from a Rails Engine without having to duplicate code using alias_method
module ValueSets
SetsController.class_eval do
def new_with_authorize
new_without_authorize
authorize #value_set
end
alias_method :new_without_authorize, :new
alias_method :new, :new_with_authorize
end
end

Change ruby on rails controller to respond differently based on route nesting

Is it possible to have a controller that interacts in a standard way at both the top level and also the nested level? Or will static routes need to be configured?
When I visit the first address /list/:list_id/items I want it to follow the nested_index method to display only a subset of the listed items (The items that belong to the list).
http://localhost:3000/list/:list_id/items
When I visit the below (/items) address I want it to show the whole list of items.
http://localhost:3000/items
/app/controllers/items_controller.rb
def index
#Item = Item.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #Item }
end
end
def nested_index
#list = List.find(params[:list_id])
#items = #list.items.paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #list }
end
end
/config/routes.rb
AppName::Application.routes.draw do
resources :list do
resources :items
end
end
# Do I need to add further routes here?
Personally, I think you should split this out into two separate controllers.
The index method of your controller should be designed to do just one thing. In the case of your nested route it should be fetching all the items appropriate for the selected list and passing them to the appropriate view. In the other instance it is fetching all items and (probably) passing them to a completely different view.
It seems you're trying to get one controller to do the job of two, simply for the sake of the controller's name.
I'd suggest creating an apps_controller and use that to collect all your items and display them, and leave your items_controller for its nested use.
Remember you don't need to name a controller after the model it interacts with ... rather, you should name it after the function it is responsible for. A controller which receives an activation code for a user account might update an is_active boolean on a User model, but you would call this controller Activations since that is what it does.
If you have lots of overlap between controllers you can move their code into modules and then include those modules in both controllers. This way you can DRY up your code whilst keeping the logic separate where necessary.
Take a look at these links for some ideas on code extraction:
http://railscasts.com/episodes/398-service-objects
http://railscasts.com/episodes/416-form-objects
But before you start refactoring all of your code into modules ... consider whether it adds anything to your codebase. Does it make things simpler? Does it make things more readable? Does it save you anything other than typing out a few more lines? If there's no benefit to refactoring ... just don't do it.
#Jon is right. This should be split into several different controllers:
# app/controllers/items_controller.rb
class ItemsController < ApplicationController
# default RESTful actions to operate on lists, for example #index
def index
#Item = Item.all
respond_to do |format|
format.html
format.json { render json: #item }
end
end
end
# app/controllers/lists_controller.rb
class ListsController < ApplicationController
# default RESTful actions to operate on lists
end
# app/controllers/lists/items_controllers.rb
class Lists::ItemsController < ApplicationController
def show
#list = List.find(params[:list_id])
#items = #list.items.paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.html
format.json { render json: #items }
end
end
end
Routes:
AppName::Application.routes.draw do
resources :items
resources :lists do
resources :items
end
end

Rails way to define parameter for filter

There is the following routes:
namespace :api do
namespace :v1 do
resources :places, only: [:index]
end
end
The code of the controller:
class API::V1::PlacesController < API::V1::ApplicationController
def index
#places = (!params[:id]) ? Place.all : Place.find_all_by_type_id(params[:id])
respond_to do |format|
format.json { render json: #places }
format.html
end
end
end
'Place' has 'type_id' field, and I want to filter places by its filter_id. As you can see, now I send the parameter through URL as "places?id=1". But may be I must send parameter as "places/1"? I need also to set up paths; now they doesn't work with "?id=1" form. Please, tell me, how should I do? Thanks.
Rails convention would be to have the list of the places in the "index" action mapped to the relative path /places (GET method).
And then /places/1 (GET) would be mapped to "show", which is intended for presenting a member of the collection. For "show", the route would assign the ID segment of the path ("1") to params[:id].
The guides have a table of default route mappings. The :type_id attribute in the model vs. the :id attribute in the route probably confused you.
A simple solution would be to use /places?type_id=1 instead. In your controller, you can have something like:
def index
collection = Place.all
collection = collection.where(:type_id => params[:type_id].to_s) unless params[:type_id].to_s.blank?
respond_to do |format|
# ...
end
end
Setting :type_id as a query parameter instead of integrating into the relative path seems especially reasonable to me since you are building an API and might add support for more filters in the future.
My recommendation is to rewrite it like this:
# Your routes
namespace :api do
namespace :v1 do
resources :places, only: [:index]
get "/places/by_type/:type_id" => "places#by_type", as: :places_by_type
end
end
# Your controller
class API::V1::PlacesController < API::V1::ApplicationController
def index
respond_to do |format|
format.json { render json: #places }
format.html
end
end
def by_type
#places = Place.where(type_id: params[:type_id])
respond_to do |format|
format.js { render json: #places }
format.html do
render action: "index"
end
end
end
end
I could be slightly wrong about the routes, but I'm pretty sure it should work.

New to rails. Index action doesnt like my initialization method.. Why?

I am completely new to rails and playing with the code to make pages work.
The link localhost:3000/zombies/1 works (show action)
but localhost:3000/zombies (index action) doesn't. Below are my routes and controller:
ROUTES ARE:
resources :zombies
CONTROLLER is:
class ZombiesController < ApplicationController
before_filter :get_zombie_params
def index
respond_to do |format|
format.html # index.html.erb
format.json { render json: #zombies }
end
end
def show
#disp_zombie = increase_age #zombie, 15
#zombie_new_age = #disp_zombie
respond_to do |format|
format.html # show.html.erb
format.json { render json: #zombie }
end
end
def increase_age zombie, incr
zombie = zombie.age + incr
end
def get_zombie_params
#zombie=Zombie.find(params[:id])
#zombies = Zombie.all
end
end
Why is this?
Editing answer based on the comment
I get a page with the error: ActiveRecord::RecordNotFound in
ZombiesController#index Couldn't find Zombie without an ID Rails.root:
C:/Sites/TwitterForZombies Application Trace | Framework Trace | Full
Trace app/controllers/zombies_controller.rb:85:in `get_zombie_params'
The url, localhost:3000/zombies which calls index action does not include id parameter.
That's why the app is failing at #zombie=Zombie.find(params[:id]).
If you want to fix this issue, use before_filter on show action only.
before_filter :get_zombie_params, only: :show
And insert this into index action as I have originally suggested.
def index
#zombies = Zombies.all
...
end
This is happening because when you define resources :zombies, you get these routes :
/zombies
/zombies/:id
Therefore when navigating to /zombies you don't have a params[:id], it is nil
Zombie.find method will raise an error if it can't find any record with the given id and halt further processing of your code.
You can use Zombie.find_by_id if you don't want an exception raised when there is no result.
But I don't think that this what you want here, you'd rather define a get_zombie_by_id method and a get_all_zombies method and separate the code from your get_zombie_params
Then you would have to define which method should be called before what action by changing your before_filter like so, in your case :
before_filter :get_zombie_by_id, :only => :show
before_filter :get_all_zombies, :only => :index
This way Zombie.find(params[:id]) will only get called when on the show action.
You can also use :except to do the opposite.
it does work because you need to send back ( to your index view ) the list of your zombies.
The get_zombie_params() excutes right but does not send #zombies to the index() action.
you need to do :
def index
#zombies = Zombie.all
#... the rest of the code
end

Rails 3: Two different layouts with the same controller and action?

Suppose you want a Blog with two different layouts. One layout should look like a conventional Blog with a header, a footer, a menu and so on. The other layout should only contain the blog posts and nothing more. How would you do that without losing the connection to the model, forcing the execution and rendering of only one action and prevent to repeat yourself (DRY)?
posts_controller.rb
class PostsController < ApplicationController
layout :choose_layout
# chooses the layout by action name
# problem: it forces us to use more than one action
def choose_layout
if action_name == 'diashow'
return 'diashow'
else
return 'application'
end
end
# the one and only action
def index
#posts = Post.all
#number_posts = Post.count
#timer_sec = 5
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
# the unwanted action
# it should execute and render the index action
def diashow
index # no sense cuz of no index-view rendering
#render :action => "index" # doesn't get the model information
end
[..]
end
Possibly I want to go the wrong way, but I can't find the right one.
Update:
My solution looks like this:
posts_controller.rb
class PostsController < ApplicationController
layout :choose_layout
def choose_layout
current_uri = request.env['PATH_INFO']
if current_uri.include?('diashow')
return 'diashow'
else
return 'application'
end
end
def index
#posts = Post.all
#number_posts = Post.count
#timer_sec = 5
respond_to do |format|
format.html # index.html.erb
format.json { render json: #posts }
end
end
[..]
end
config/routes.rb
Wpr::Application.routes.draw do
root :to => 'posts#index'
match 'diashow' => 'posts#index'
[..]
end
Two different routes are pointing at the same location (controller/action).
current_uri = request.env['PATH_INFO'] saves the url into a variable and the following if current_uri.include?('diashow') checks if it is the route we configured in our routes.rb.
You would select which layout to render depending on a certain condition. For example, a parameter in the URL, the device in which the page is being rendered etc.
Just use that condition in your choose_layout function, instead of deciding the layout on the basis of action_name. The diashow action is unnecessary.

Resources