Is there a way to have page numbers as alphabets in rails - ruby-on-rails

In my rails application I wanted to use pagination for names. I'm fetching the names from a postgresql table.
def index
#users = User.order("name").paginate(:page=>params[:page],:per_page=>50)
end
Is there a way of alphabetic pagination that has page numbers as a,b,c..........z
Thanks for your help,
Ramya.

Im doing this too in one of my projects. I didnt use a gem for this. Its really easy using Ranges... For example:
<% ('A'..'Z').each do |char| %>
<% if char==params[:char] %>
<%= link_to :action => 'your_action', :char => char, :class => 'selected_char' %>
<% else %>
<%= link_to :action => 'your_action', :char => char %>
<% end %>
<% end %>
Then in the controller Action you select the Objects by the params[:char] given. For example:
#instance_var = ModelName.where("field LIKE ?", "#{params[:char]}%")
this did it for me. You can easy build a partial out of this and use it for many different Models.

Related

Rails delete_if using hashes to ignore current article (Middleman) revisited

I've got an easy one for you guys.
I want to have a featured content section where the current article link is hidden or removed
So already this works using Middleman Blog with delete_if:
<% blog(content).articles.delete_if{|item| item == current_article}.each do |article| %>
<%= article.href %>
<% end %>
However I'm using Middleman proxies so I don't have access to the current_article method...
I've got an YAML structure that holds the following mock data (amongst others) with the folder setup like: data > site > caseStudy > RANDOM-ID423536.yaml (Generated by a CMS)
Inside each yaml file you'll find stuff like:
:id: 2k1YccJrQsKE2siSO6o6ac
:title: Heyplace
My config.rb looks like this
data.site.caseStudy.each do | id, this |
proxy "/cases/#{this.slug}/index.html", "/cases/cases-template.html", :locals => { this: this }, :ignore => true
end
My template file holds the following
<%= partial(:'partials/footer', :locals => {:content => data.site.caseStudy, :title => 'See more projects'}) %>
My loop that gets content
<% content.each do |id, this| %>
<%= partial "partials/tile" %>
<% end %>
So I guess my question is, how can I use delete_if in this instance so that the current article isnt being displayed?
I have tried stuff like, but to no avail:
<% content.delete_if{|e| e.title == content.title}.each do |id, article| %>
<%= article.href %>
<% end %>
As well as:
<% content.reject{|key, value| key >= this.id }.sort_by{ |index| rand }.each do |id, this| %>
<%= partial "partials/tile" %>
<% end %>
(This deletes the data all together forever until you reload the page, strange!)
Any help is appreciated! Thank you!

rails delete_if using hashes to ignore current article (Middleman)

I've got an easy one for you guys.
I want to have a featured content section where the current article is EXCLUDED
So this works using Middleman Blog with delete_if:
<% blog(content).articles.delete_if{|item| item == current_article}.each do |article| %>
<%= article_content %>
<% end %>
However I'm using Middleman proxies so I don't have access to the current_article method...
I've got an YAML structure that holds the following mock data (amongst others) with the folder setup like: data > site > caseStudy > RANDOM-ID423536.yaml (Generated by a CMS)
Inside each yaml file you'll find stuff like:
:id: 2k1YccJrQsKE2siSO6o6ac
:title: Heyplace
My config.rb looks like this
data.site.caseStudy.each do | id, this |
proxy "/cases/#{this.slug}/index.html", "/cases/cases-template.html", :locals => { this: this }, :ignore => true
end
My template file holds the following
<%= partial(:'partials/footer', :locals => {:content => data.site.caseStudy, :title => 'See more projects'}) %>
My loop that gets content
<% content.each do |id, this| %>
<%= partial "partials/tile" %>
<% end %>
So I guess my question is, how can I use delete_if in this instance so that the current article isnt being displayed?
I have tried stuff like, but to no avail:
<% content.delete_if{|e| e.title == content.title}.each do |id, this| %>
<%= partial "partials/tile" %>
<% end %>
Any help is appreciated! Thank you!
Solved, ended up doing
<% content.reject{ | id, item| item == this }.each do |id, this| %>
<%= partial "partials/tile", :locals => { :this => this, :dir => "/#{this.content_type_id.downcase}/", :secondary => 'View' } %>
<% end %>
Courtesy of #David Litvak
I'm the maintainer of contentful_middleman. Looks like you could just send the this to the partial as local, then iterate through your content and make sure that you exclude the one that matches the id of this.

Rails acts_as_taggable_on Tag Link Filtering

I have a list of 'notes', and each note has some tags via acts_as_taggable_on. It's a great plugin, and the tags are working wonderfully.
What would be the best way to filter this list of notes by the tag that is clicked on?
Example:
<% #notes.each do |note| %>
<%= note.content %>
<% note.tag_list.each do |tag| %>
<%= link_to tag, '#', :class => "tag" %>
<% end %>
<% end %>
What should I replace the '#' with in order to change or scope out #notes? Not too familiar with this.
EDIt: I want something just like StackOverFlow actually, how would I add parameters to the URL based on the link?
Thanks!
I believe I figured it out. Was just having an off-moment.
I can create a named route like:
match 'tags/:tag' => 'controller#index', :as => 'tag'
And that way I can get the parameter I need.

How add logic to Views? Ruby on Rails

Right now I'm building a project management app in rails, here is some background info:
Right now i have 2 models, one is User and the other one is Client. Clients and Users have a one-to-one relationship (client -> has_one and user -> belongs_to which means that the foreign key it's in the users table)
So what I'm trying to do it's once you add a client you can actually add credentials (add an user) to that client, in order to do so all the clients are being displayed with a link next to that client's name meaning that you can actually create credentials for that client.
What i can't figure it out how to do is, that if you actually have credentials in the database (meaning that there's a record in the users table with your client id) then don't display that link.
Here's what i thought that would work
<% for client in #client%>
<h5>
<h4><%= client.id %></h4>
<%= client.name %>
<% for user in #user %>
<% if user.client_id = client.id %>
Credentials
<%end%>
<% end %>
</h5>
<% end %>
And here's the controller:
def index
#client = Client.find_all_by_admin(0)
#user = User.find(:all)
end
but instead it just puts the link the amount of times per records in the user table. Any help?
You can do the following
# controller
def index
#clients = Client.find_all_by_admin(0, :include => :user)
end
# view
<% #clients.each do |client| %>
<h5>
<h4><%= client.id %></h4>
<%= link_to client.name, {:action => 'show', :id => client.id} %>
<% if client.user.blank? %>
<%= link_to "Credentials",
{:controller => 'user', :action => 'new', :client_id => client.id} %>
<% end %>
</h5>
<% end %>
Edit
Better solution, getting in the controller only the clients who doesn't have a user yet:
# model
class Client < ActiveRecord::Base
has_one :user
named_scope :without_user,
:conditions => "id NOT IN ( SELECT client_id FROM users )"
end
# controller
#clients = Client.without_user.find_all_by_admin(0)
# view
<% #clients.each do |client| %>
<h5>
<h4><%= client.id %></h4>
<%= link_to client.name, {:action => 'show', :id => client.id} %>
<%= link_to "Credentials",
{:controller => 'user', :action => 'new', :client_id => client.id} %>
</h5>
<% end %>
Just to add to j.'s answer, you really should use link_to view helper instead of hardcoding URLs.
I haven't worked with Rails in sometime, but two things:
There are helpers to make your urls, it's easy to make mistakes. (Not that I see one, but just to let you know about them...)
Views should not have logic. As much as you can get away put it in the controller. Or even better in the model.
It's a good practice to not use "User.find :all", as your app grows this will kill you.
It's better to have a method in the model that gets just the info you will use. Databases are better in this case to filter.

How do I run an array through an IF statement in rails?

I am creating an application that highlights user messages from a stream based on whether or not the user has been 'vouched'. It works fine if it's setup for a single author. For example
controller: #vouch = Vouch.last.vouched_user_nickname
view:
<% Twitter::Search.new(params[:id]).each do |tweet| %>
<li>
<%= image_tag tweet.profile_image_url %>
<% if #vouch.include? tweet.from_user %> <div class="flit_message_containerh">
<u> <%= tweet.from_user %></u> <%= linkup_mentions(auto_link(h tweet.text)) %>
<div class="time_ago">
<%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
<% else %> <div class="flit_message_container">
<u> <%= tweet.from_user %></u>
<%= linkup_mentions(auto_link(h tweet.text)) %>
<div class="time_ago">
<%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
<% end %>
But I'm having trouble doing it for multiple user nicknames.
#vouch = Vouch.find(:all,
:select => "vouched_user_nickname",
:group => 'vouched_user_nickname'
)
Any ideas would be greatly appreciated. I'm a rails noob.
Assuming there isn't a relation between your Vouch model and the Twitter source (I haven't used that gem/plugin yet so I don't know), one solution is to pull all the Twitter entries you want and all the vouches in the controller and do the check in the view.
controller:
#tweets = Twitter::Search.new(params[:id])
#vouches = Vouch.find(:all)
view:
<% #tweets.each do |tweet| %>
<div class="flit_message_container<%=
#vouches.any? { |v| v.vouched_user_nickname == tweet.from_user } ? "h" : ""
%>">
...
</div>
<% end %>
#vouch = Vouch.find_by_vouched_user_nickname(:all, ["nickname1","nickname2"])
Your problem seems to be that you are not looping through the array, so how can it decide if certain elements meet the criteria you set?
Example, in your view:
<% for vouch in #vouch do %>
<% if vouch.include? tweet.from_user %>
<div class="flit_message_containerh">
<u> <%= tweet.from_user %></u> <%= linkup_mentions(auto_link(h tweet.text)) %>
<div class="time_ago">
<%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
<% else %> <div class="flit_message_container">
<u> <%= tweet.from_user %></u>
<%= linkup_mentions(auto_link(h tweet.text)) %>
<div class="time_ago">
<%= link_to distance_of_time_in_words_to_now(tweet.created_at) , tweet %>
<% end %>
<% end %>
I see several problems.
The first one is this:
#vouch = Vouch.last.vouched_user_nickname
You are using a variable called #vouch to store a user nickname. That is counterintuitive and will confuse other people reading your code (like myself). Use something like this instead:
#vouch = Vouch.last #on the controller
#vouch.vouched_used_nickname #on the view
This ... eum ... "exotic" naming convention helps confusing yourself when you try to do the "multiple" example:
#vouch = Vouch.find(:all,
:select => "vouched_user_nickname",
:group => 'vouched_user_nickname')
Activerecord's find(:all, ...) will allways return an array of activerecord objects (or an empty array). You seem to be expecting an array of strings. You will allways get Vouches if you do Vouch.find.
The :select part just limits the amount of information these vouches have (they only come with vouched_user_nickname populated. The rest, including their id, is empty, because it is not read from the database).
If you want to have an array of user nicknames you can do it like this:
# note the names. #vouchers in plural, and #nicknames for the user names
#vouchers = Vouch.find(:all, :select => "vouched_user_nickname",
:group => 'vouched_user_nickname')
#nicknames = #vouchers.collect{|v| v.vouched_user_nickname}
Is your problem that you don't know the correct controller code to write to find the #vouch array? Or is it that you don't know what to do with the array once you get it?
view: <% if #vouch.include? tweet.from_user %>
.include? is a method you can call on either a single object or an array of objects if tweet.from_user has an object that is also included in the #vouch array to get the #vouch array in your controller you should:

Resources