Rails 5 - Render 2 different post orders on different URLs - ruby-on-rails

I'm sure this is simple but I'm super new to Rails and just can't find the answer from googling.
I have a posts model by default that will be sorted by a custom algorithm (currently sorted by asc). I also want to create another view where it's sorted by newest under mydomain.com/recent
I don't want this done via ajax or anything. I just want the ability to show different views on separate url paths.
posts_controller.erb
def index
#posts = Post.order('created_at ASC').paginate(:page => params[:page], :per_page => 15)
respond_to do |format|
format.html
format.js
end
end
index.html.erb
<%= render #links %>

You could just use scopes in the model and parameters in the controller.
For example if your model had a scope like:
class Post
# Move current controller custom order to scope
scope :my_custom_order, -> { order(created_at: :asc }
scope :recent_order, -> { order(created_at: :desc) }
end
And if you made the /recent part of your URL an optional parameter in your routes.rb file like:
scope '(:order)' do
# Do all your routes to route to PostsController in here
end
Then in your controller you could use that to determine your order for posts, and default back to your custom order for it:
def index
#posts = Post.send(order).paginate(page: params[:page], per_page: 15)
respond_to do |format|
format.html
format.js
end
end
private
def order
:"#{params[:order] || 'my_custom'}_order"
end
Something along those lines with some tweaks to make it work exactly for your project should get you started on what you want to achieve.

Related

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: overriding as_json for dynamic value -- is there a smarter way?

I want to output a list of affiliate links, each tagged to identify the current user. It would be simple in HTML, but we're writing an API, so the output is JSON.
I have it working, but it seems overly complicated. Is this the best approach?
My model, AffiliateLink contains a field (the raw HTML of the link) that I'll transform and output on the fly by adding a token. I have a model method that produces the replacement -- it is non-trivial because we use multiple affiliates and each has a special transformation rule that this method knows about:
def link_with_token(user_token)
# some gnarly code that depends on a lot of stuff the model knows
# that returns a proper link
end
To get my correct link html in JSON I have done these things:
add attr_accessor :link_html to model
add an instance method to set the new accessor
...
def set_link_html(token)
self.link_html = link_with_tracking_token(token)
end
override as_json in the model, replacing the original html_code with link_html
...
def as_json(options = {})
super(:methods => :link_html, :except => :html_code)
end
iterate over the collection returned in the controller method to do the transformation
...
def index
#links = Admin::AffiliateLink.all # TODO, pagination, etc.
respond_to do |format|
format.html # index.html.erb
format.json do
#links.each do |link|
link.set_link_html(account_tracking_token)
end
render json: #links
end
end
end
This seems like a lot of stuff to do just to get my teensy-weensy transformation done. Helpful suggestions (relating to this problem and not to other aspects of the code, which is in flux now) are welcome.
1) A quick solution to your problem (as demonstrated here):
affiliate_links_controller.rb
def index
#links = Admin::AffiliateLink.all # TODO, pagination, etc.
respond_to do |format|
format.html # index.html.erb
format.json do
render json: #links.to_json(:account_tracking_token => account_tracking_token)
end
end
end
AffiliateLink.rb
# I advocate reverse_merge so passed-in options overwrite defaults when option
# keys match.
def as_json(options = {})
json = super(options.reverse_merge(:except => :html_code))
json[:link_with_token] = link_with_token(options[:account_tracking_token])
json
end
2) A more hardcore solution, if you're really writing an API:
See this article describing your problem.
See the gem that the authors made as a solution.
See this railscast on using the gem.
3) And lastly, the convenient solution. If you have a convenient model relation, this is clean:
Pretending AffiliateLink belongs_to :user. And assuming user_token is an accessible attribute of User.
AffiliateLink.rb
# have access to user.user_token via relation
def link_with_token
# some gnarly code that depends on a lot of stuff the model knows
# that returns a proper link
end
def as_json(options = {})
super(options.reverse_merge(:methods => :link_with_token, :except => :html_code))
end
affiliate_links_controller.rb
def index
#links = Admin::AffiliateLink.all # TODO, pagination, etc.
respond_to do |format|
format.html # index.html.erb
format.json do
render json: #links
end
end
end

Is there any way to order the results of a Cancan accessible_by call

I am using the Cancan accessible_by to retrieve a ActiveRecord::Relation result (example code below). Is there any way to order the results during the accessible_by call?
UPDATE: Srdjan's was correct. #attributes was already being set using accessible_by. I have updated the example to show the sort by User's login. Attribute has a belongs_to relationship with User.
class AttributesController < ApplicationController
load_and_authorize_resource
def index
#attributes = #attributes.includes(:user).order("#{User.table_name}.login")
end
# GET /attribute/1
# GET /attribute/1.xml
def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #attribute }
end
end
end
From the horse's mouth, as it were.
#articles = Article.accessible_by(current_ability, :update)
This is an Active Record scope so other scopes and pagination can be chained onto it.
Source: https://github.com/ryanb/cancan/wiki/Fetching-Records
Also, on the top of that page, you'll note that as of CanCan 1.4, this is done automatically when you call load_resource. Are you on that version?

I simple search controller that stores search history, should I use resource routing or non-resource?

I am learning rails and am toying with a simple web-app that integrates with flickr to search photos based on user given criteria and store the query in a search history table.
I am seeking the best or 'rails' way of handling this. Should I setup a controller and non-resource routes that handle the search and store the data in a custom table; or should I create a resource for queries with a resource route and an additional path for search?
Lemme refactor a bit:
rails g controller searches
This neat rule will allow you to use nifty action-per-resource searches controller:
# config/routes.rb
get 'search/:action' => 'searches#:action'
Here's simplified version of such one:
class SearchesController < ApplicationController
def foos
search do
Foo.where :name => params[:q]
end
end
def bars
search do
Bar.where :title => params[:q]
end
end
private
def search(&block)
if params[:q]
#results = yield if block_given?
respond_to do |format|
format.html # resources.html.erb
format.json { render json: #results }
end
else
redirect_to root_url, :notice => 'No search query was specified.'
end
end
end
Examples of URL queries:
/search/foos?q=baz
/search/bars?q=baz
My thoughts:
In config/routes.rb:
match '/search/:query' => 'search#search', :as => 'search'
Create a SearchController:
rails generate controller search
In app/controllers/search_controller.rb:
class SearchController < ApplicationController
def search
# Use params[:query] to perform the search
end
end
For example, a query for "apples" would appear as: http://example.com/search/apples
You can generate links to queries with: search_path('apples')

Rails: include related object in JSON output

I have a note class that belongs to a user (ie a user can create many notes).
clip from my notes controller
class NotesController < ApplicationController
before_filter :authenticate_user!
respond_to :html, :xml, :json
# GET /notes
# GET /notes.xml
def index
#notes = Note.includes(:user).order("created_at DESC")
respond_with #notes
end
When I ask for the index in json results for example /notes.json, it returns the notes but only returns user_id for the user object. I would like it to also include user.username (and would be curious how to have the whole user object embedded).
Bonus question: I could not find a way to make the column show as author_id and have it relate back to user. If this is easy to do, how do you do it?
I'm not sure the new respond_to/respond_with style is flexible enough to do this. It very well may be, but as far as I understand, it's meant to simplify only the simplest cases.
You can achieve what you are trying to do with the old-style respond_to with a block, however, by passing parameters to to_json. Try something like this:
class NotesController < ApplicationController
def index
#notes = Note.order("created_at desc")
respond_to do |format|
format.json do
render :json => #notes.to_json(:include => { :user => { :only => :username } })
end
end
end
end
You can also use Jbuilder(https://github.com/rails/jbuilder) to response with data very flexible.
#notes = Note.includes(:user).order("created_at DESC")
and in your index.json.jbuilder file, you can
json.extract! #note
json.username #note.user.username
Would it be possible to do it the other way around?
def index
#user = User.includes(:notes).order("created_at DESC")
respond_with #user
end
It would be expensive to include user objects each time the #notes is iterated.

Resources