Question about checkbox on Ruby on Rails - ruby-on-rails

I'm developing a website for my University and I got a problem.
There is a page where I have the list of all students of the university. The admin can select some students that will be able to go to a selective process and then he have to see them in other separate page.
How can I do that using Ruby On Rails?
Thanks,
Hugo Henley

Hi If you want use checkboxes you should write inside your form something similar to <td><%= check_box_tag "user_ids[]", "#{user.id}", true%><%= user.name%></td> then you'll get array od user ids as an params[:user_ids] and you may show only this users on other page

<h1>in your view </h1>
By checking those ones using checkboxes you can get the id's of those students like this
<%= check_box_tag "user_ids[]", "#{user.id}", true%>
passing those id's into the respective controller action
#users = User.where(:id => params[:user_ids])
Display those object details in to the required webpage using #users.each
<% #users.each do |user| %>
<%= user.name %>
<%end%>

Related

How do display activity owner name in Angular?

I'm trying to build a activity stream in my rails/angular application using the Public Activity gem and the Railscast. It's a pretty easy set up on the rails side, but I'm having some trouble with the angular side of things.
I've tracked my movie model. So when a movie is added it's recorded as a new activity. I've created a template, service and controller (on the angular side) to display the activity.
The template,
%ul{"ng-repeat" => "activitie in activities"}
%li {{ activitie }}
When I view the template in my app it displays like this (which is expected).
{"id":2,"trackable_id":5,"trackable_type":"Movie","owner_id":1,"owner_type":"User","key":"movie.create","parameters":{},"recipient_id":null,"recipient_type":null,"created_at":"2015-12-30T11:55:06.766Z","updated_at":"2015-12-30T11:55:06.766Z"}
Ofcourse this is not how I want to display the data, but here it gets tricky. In the railscast Ryan adds this code to his template,
<% #activities.each do |activity| %>
<div class="activity">
<%= link_to activity.owner.name, activity.owner if activity.owner %>
<%= render_activity activity %>
</div>
<% end %>
So he uses activity.owner.name to present the name. But I have no idea how to get this result in Angular. The owner_id is the id of the user that added the movie, but I don't know how to display it through Angular.
You can join with the users table to incorporate the users name.
PublicActivity::Activity.
joins("INNER JOIN users ON users.id = activities.owner_id").
select("activities.*, users.name")
Now you should be able to call activity.name.

Limit check_box_tags in rails application

I'm using the check_box_tag to setup checkboxes in my rails app (in an html.erb file, within a form) and i want to limit the user to selecting a maximum of 2. How can i achieve this?
<% if #users != nil %>
<% #users.each do |u| %>
<%= check_box_tag 'user_ids[]' , u.id %>
<%= label_tag 'user_ids[]', u.name %> <br>
<% end %>
<% end %>
There's no way to do it at the time of creating your checkboxes - ie, in the code you've pasted above.
You'd have to check it in the browser using javascript - when the user checks more than two checkboxes, you can show some kind of error in the browser.
You should also check the limit of two checkboxes server-side - when your form is submitted, you can return an error and not save the changes if more than two checkboxes are checked.
But just to re-iterate, it's not a feature that's built in to html itself, so when you're creating your checkboxes in the view file, you can't limit it to 2 at that point.

Dynamically adding pages to rails app

This may be a more fundamental aspect of Rails but I am very new.
I have a basic app which is sort of a basic craigslist clone.
Users - I used devise for this, users can sign up/sign in/sign out/edit their profile.
Listings - Users can add multiple listings. Currently I have a few basic fields populating the database (title, content, phonenumber, price, location).
What I want to be able to do is the following:
I want to have a page which lists all of the ads. Currently I can do this by accessing the database and displaying all the contents. Like this:
<h2>LISTINGS</h2>
<% #users.each do |u| %>
<% u.listings.each do |i|%>
<%=u.email %>
<%=i.title %>
<%=i.content %>
<%=i.number %>
<%=i.price %>
<%=i.location %>
<% end %>
<% end %>
What I want to do is have this page only list the titles, each title would link to the appropriate ad. Users could then access their ad with a URL. Ideally the URL would be the title similar to what stackoverflow does "IE in the URL the title is included with "-" instead of spaces" but that is a minor concern at this point.
How is the best way to go about doing this? I may be using incorrect terminology here as I am having trouble finding information.
I think that you should use as below code -
Eg :
<% #users.each do |u| %>
<%= link_to u.name, :controller => 'user', :action => 'show', :id => u.id %>
<% end %>
I hope that you can solve your problem by using this way.

How to create a rails checkbox form?

Im trying to create a checkbox table of about 20 "interests" that lets the user select as many as they want. I have a Interest & User model with a HABTM relationship (through a "interests_users"join table).
So:
How do i seed the interests table (just has a name:string attribute) with the names of 20 or so pre set interests?
How do i display these in a ERB form allowing the user to select as many as they like?
Note.. Im using the Wicked gem to create a multistep form (<-working great)
If you're on Rails >= 3.0, then have a look the db/seeds.rb file. You get to put arbitrary Ruby code in that file, which you run through the Rake task rake db:seed. You can just put a lot of lines like Interest.create :name => 'World Domination'.
This one is going to depend on how you set up your form. Going off the information you've given, I'd do something like this:
<%= form_for #user do |f| -%>
<% Interest.all.each do |i| -%>
<div><%= i.name -%> <%= check_box_tag 'user[interests][]', i.id, #user.interests.detect{|ui| ui.name == i.name} -%></div>
<% end -%>
<% end -%>
In your controller you would then be able to just update your user model's attributes. Be sure to make sure you are able to mass-assign your parameters, and also keep in mind a limitation of the HTML spec with regard to unchecked checkboxes (read the part titled, "Gotcha").
EDIT: fixed some grammar-related typos.
<% for interest in Interest.find(:all) %>
<%= check_box_tag "user[interest_ids][]", interest.id, #user.interests.include?(interest) %>
<%= interest.name %>
<% end %>

Only show content when certain criteria is met?

I'm wondering if theres a best practice for what I'm trying to accomplish...
First we have the model categories, categories, has_many posts.
Now lets say, users add posts.
Now, I have a page, that I want to display only the current user's posts by category.
Lets say we have the following categories: A, B, and C
User 1, has posted in Categories A and B.
In my view I have something like:
#categories.each do |category|
category.name
#posts.each do |post|
if post.category_id==category.id
post content here
end
end
end
The problem with this, is I'm going to show the empty category, as well as the categories that do have content.
Is there a more efficient way of going about this? As I don't want to show the empty categories.
Best,
Elliot
UPDATE:
So I've been trying to use this code:
0}.each do |category| %>
For the most part its almost there. The issue is, it will still show an empty category if any posts have been entered in it at all (even if the current user has not input posts into that category.
So the question boils down to:
How do I make the following loop only count posts in the category from the current user?
0}.each do |category| %>
<% #categories.select {|cat| cat.posts.count > 0}.each do |category| %>
<%= category.name %><br/>
<% category.posts.select {|post| post.user == current_user}.each do |post| %>
<%= post.content %><br/>
<% end %>
<% end %>
This renders each category with any posts, then the content for each post within the category belonging to the current user. You'd probably want to do the initial selection in the controller though, to keep the view clean.
Adding to zetetic's answer, perhaps the lookup of the posts of a user in a given category would be done the opposite way. Instead of querying "All the posts for the category where the author is the current user", ask for "all the posts for the user where it is in given category"
<% #categories.select {|cat| cat.posts.count > 0}.each do |category| %>
<%= category.name %><br/>
<% current_user.posts_in_category(category).each do |post| %>
<%= post.content %><br/>
<% end %>
<% end %>
And throw a scoped search User#posts_in_category
EDIT: Probably you should also set the #categories variable already filtered from your controller, if you're not showing them somewhere else in your view. Also, if the category has no posts, it will not enter the cycle, so maybe the select is not needed.
#categories.select do |category|
category.posts.any? {|post| post.user == current_user }
end
Will filter out the categories to have only categories with posts by the current_user. I guess it would be more efficient doing this in the db, use this if you're fetching the #categories anyway.
As per my concern you need to have an intermediate table for users who publish posts for a given category that table might have following fields
user_id,post_id, category_id by this way you will be having a table which has category id's and for each category there is a post.
then you can do the following
get the distinct categories (from above table)
loop through the categories
get posts for those categories
** then you will not get any categories without posts
cheers,
sameera

Resources