User profile with devise, checklist app - ruby-on-rails

I am new to rails and I am currently in the process of developing a check list app. I would greatly appreciate some guidance as I am currently suck in my development process and would just like some help getting me on my way.
Goal:
Admins will have the privileges to add collections, and add products to those specific collections - (all will be pre-populated and defined before the site goes live).
Users should arrive at the homepage, be presented with a splash page of what the page is and be able to sign in/ up. Once signed in the user should be directed to their profile page.
a.) first time there, they should be presented with a list of collections they want to 'follow" / "watch" (that show up on their profile page to track the products they are missing from the entire collection)
b.) second time there, they should be presented with the collections they are watching, and the all products in that collection.
Once on their profile page, they should be able to "check" and "uncheck" products in the collection. I want to show all the products in the collection regardless if they have them or not, and they can check the ones they have, and I will do some fancy front side stuff to make it visually appealing. (fade from black / color - on true/false value - animate all selected ones to front of container..ect)
What I have:
I have a Collection(has_many) -> Products(belongs_to) association models set up. I have both of the controllers CRUIDified, and the product page is CRUIDified through association with collection. (nested routes / #collection.products.build etc.)
I have a generated Devise User model with email confirmation. I gave that model a User(has_many) -> Collections(belongs_to) association.
My next steps?
I am trying to assign a user to a profile page that I can display the results of the their collections/products. I am stuck as to how to achieve this. Do I need to create a user controller and put a before_filter :authenticate_user! and limit the actions I don't want accessible by normal users? Or do I need to generate a new Model Profile, and put an association there?
If you would like to see my current code it can be found here:
https://github.com/gogogarrett/Blind-Boxd
Thanks in advance,
Garrett

If you want to have a page for signed in users to see their collections and products, you don't necessarily need that to be in a User Controller.
I have put my user overview pages in a pages_controller. You then have a before filter of :authenticate_user!, and you just pass whatever you need into the view (#collections = current_user.collections).
It doesn't sound like you need a new model.

Related

Let user create page in rails

I am working on an e-commerce application. Users can log in and create their own custom store. I have a Shop model, and User has an association with shop model. The show action renders the show page of shop.
I want to allow Users to create other static pages like About and Contact.
Any suggestions would be highly appreciated.
These sound like they would be a set of HTML, a title, a shop with which they are associated, and you'd just call it StaticPage.
I think that show and edit actions on StaticPage would give you the functionality you need to make this work.

create a profile page for user to view as if logged out

I'm making a Rails application using Devise. On the user profile page, it provides links for the user to update and delete certain elements of their profile. The links are obviously only visible to the signed-in user whose profile it is. The one disadvantage of this is that it doesn't allow the user to view their profile page from the perspective of a visitor, unless they want to log out and navigate to their profile. Some websites offer a "view your profile" link which allows users to view a page from the perspective of someone else. Is there a way to accomplish this with Rails and Devise?
Sure. You are able to create UsersController with show action. Where you will able to display the information you want.
If you want to manage users through CRUD interface there is a wiki page that may help you.
The url to user's profile can looks like /users/:id or you can define custom route such as /user/unique-username (the last example you can achieve using friendly_id gem)

Rails: How to restrict actions to only certain users?

I am currently doing a project for uploading pics. There are users, albums, and pics. I added a friendship model so that people can friend each other like a social network. However, I noticed that I put a lot of <% if current_user.friends.include?(#user) %> in the view to check if the user of the page I'm showing is a friend of the logged in client, and therefore allowing them to have certain privileges and forms and etc.. Is there a better way or place to do this than to pollute my views with if/else statements ? Also, I don't feel like my method is very secure since someone could always manually enter the url and mess with info that they're not supposed to.
You want an authorization framework such as CanCan.
In an ability file, you configure it that a user can, say, view something or edit some other thing, only if the user is a friend of the owner. Then in the view or the controller, you can just check that the user is authorized to do the appropriate action.
For specific details about setting up an ability based on details of the models (i.e. whether the owner is a friend of the current user), go to this documentation and look for "Hash of Conditions".
Consider using something like the mosaic-access gem, which allows you to white/blacklist controllers and specific actions for the currently logged in user.

How can I pull in data from other controllers/tables into one list?

I have a rails project where users sign up and login. Users will create "Music" so I have a "Music" controller, the user will fill in a title, and upload their MP3. Every user can do this -- this is dandy.
When a user logs in I want them to see everyone's uploaded music in a nice list view at http://example.org/explore. I will have some "latest additions since your last login" somewhere on the sidebar of this "explore" page in the future among some other features.
My question is, should I create a controller called "Explore" with an index action for this listing with something like the following?
class ExploresController < ApplicationController
def index
#music = Music.all
end
def show
end
end
I thought this might be the right way to do this but it seems odd to have the "Explore" controller be using all of the actions you'd typically see in the "Music" controller. The Music controller is going to be CRUD for the current_user.
You can easily have the music#index method available to everyone and put user authentication on the CRUD methods. That way everyone can see all the uploaded music on the /music page, but only the owner of each one can alter them.
If users need an easy way to see a list of use their own mp3s then you can just use a nested resource, so it would be available at /users/:id/music.
If you need some more info on how to implement that stuff then leave a comment. The specifics would depend on what (if any) authentication systems you were using.
It doesn't look a Rails way.
Use same Music controller( if it's just a CRUD inherited_resources is your choice ) with has_scope gem and manage user access with cancan.
This way you have a 5lines MusicController and no any additional controlers
It's perfectly fine to add an 'explore' action to the Music controller.
As to whether or not a new controller is warranted, how much in common does the Explore page you have in mind have common with functions from the Music controller? Can they share partials and helpers? Will they utilize the same callbacks?
If your only plan is to list all from another model in the new controller as shown, then I would say the answer is straightforward: keep in the in the Music controller.

Ruby on rails: Creating a home page with elements pulled form different models?

Im having difficulty moving forward with my app. The problems I face are:
App: Beat selling website.
I am looking to have a home page with a : Jquery photo slider, Social bar for (twitter,Facebook,youtube), a video box, an opt-in box, and a benefits box outlining 6 benefits of the company.
I want to have all the above elements on one page (home) but have an admin interface where I can Add new photos to the photo-slider by uploading, update the video box via youtube url, manage the form for opt in and have a list of all users that have opt-in and also for the benefits i would like to be able to administer the benefits, add new one and delete old one.
So the problem I face is: I understand I have to create a controller model and view for each module I want placed on the home page. Do I need to create a MVC called HOME too? I also plan to use partials so I can pull it all together then on the main HOME page. How do I display all the modules above on the one page for the user to see, Do I need to set up some sort relationship between each controller?
I should also mention I'm using user authentication so I will have the admin pages protected.
rails version 2.3.5,
ruby version 1.8.7,
Please if you can help out it would be much appreciated,
Thanks, Ian
You seem to be slightly confused with what views do. You don't need to create a view for each model unless you want to view them separately (i.e. only that model).
What you want to do in your case is to have a controller (such as HomeController) with an index action where you gather all the data you'll to show (models, etc). Then, in app/views/home/index you use your partials to show your data with something like
<%= render 'pictures/show_inline', :picture => #picture %>
Of course, you'll need a partial in app/views/pictures/_show_inline.html.erb that will display a picture like you want it (using the picture variable we call the partial with).

Resources