I have set up devise and added a way for users to save settings or preferences. For simplicity lets say that the setting is set as a boolean in the users table.
A simple version of what I want is for a user to have a size preference.
If a user is logged in and they visit the home page (products#index) I would like them to only see products that match the setting they have selected. (obviously the product has a size field)
I also have search enabled for the products and would like to keep that functionality intact for the user using their settings
def index
#products = Product.text_search(params[:query]).page(params[:page]).per_page(50)
##products = Product.all
respond_with(#products)
end
What is the best way to go about this?
def index
#products = Product.where(size: current_user.size).text_search(params[:query]).page(params[:page]).per_page(50)
##products = Product.all
respond_with(#products)
end
Should solve your issue
Related
I have a user_id column. Instead of calling for all members how can I call up members based on current user's and user_id?
This is my controller, I tried changing .all to user_id or current_user.id plus many variations based on examples. Still can't get it. I also have no models (using authrocket). The create action also works and inserts the user_id, I have a def current_user at the bottom.
class Members::MainsController < ApplicationController
# Member Profile List
def index
#members_mains.user_id = current_user.id
#members_mains = Members::Main.all
end
private
# Common Callbacks
def set_members_main
#members_main = Members::Main.find(params[:id])
end
# White List
def members_main_params
params.require(:members_main).permit(:mfirstname, :mlastname, :mtitle, :memail, :mphone, :mnotes, :smfacebook, :smtwitter, :smlinkedin, :user_id)
end
end
If I got it right, your index action should be something like this:
# Member Profile List
def index
#current_member = Members::Main.find(current_user.id)
end
Do you intend to show a list of profiles for all members?
If not, your index action can simply be removed. If so, you wouldn't normally filter on a user_id at all for that action and you can remove that line.
To load a member profile for a specific user_id, try a show action something like this:
def show
#members_main = Members::Main.find_by(user_id: params[:id])
end
That will load a member profile based on the :id from the URL.
If you want to just show the current user's own profile instead, use current_user.id which will give you the AuthRocket user's ID.
def show
#members_main = Members::Main.find_by(user_id: current_user.id)
end
In either case, you may need to remove :show from the set_members_main callback.
Lastly, you probably want to remove :user_id from members_main_params so that users can't modify the AuthRocket user_id. You only want to control that directly (as you already are in the create action).
Hopefully that's clear enough to get you on your way, but I could be off a bit based on what functionality you actually intend for index and show.
I have installed devise and I can create users. I am trying to display all of the users so that people can see who has signed up. I would eventually like to allow people to send messages etc..(think Tinder).
Any guidance on displaying profiles to the user?
This is big question to answer here and explain .I think you want to display listing of all the users and for every user there would be a profile page(show ). what you need to do is create controller having same name as devise name.I can give you small idea about that.
Class UsersController < ApplicationController
def index
#users = User.all
end
def show
#user = User.find(params[:id]) //you can change it according to rails4 standards
end
....
....
....
end
with this you can display all user and display there profiles.
Now for messaging you need to create a different model Message and which will have fields sender,receiver and msg .both sender and receiver will be user_id ,sender you can get from session and receiver you can get from profile itself.and remember user can have many messages
But for making this real time you have to use web sockets which will be different question to answer.
I'm trying to create a mini analytics page like the one shown on the image below. It's a marketplace and each user has their analytics page with a graph. I've been able to get the views and referrer but my problem is how to go about doing the conversion part.
In my item show action i have something like this:
def show
#item = Item.find_by_rand_no params[:number]
if current_user && current_user.id != #item.owner_id
#item.views.create(item_id: #item.id, ip_address: request.remote_ip, owner_id: #item.owner_id, referrer: request.env["HTTP_REFERER"])
end
end
How do i track the ones that actually bought something so i can get a conversion rate?
Ps: A user may land on the page then click on another item and buy that instead. I also want to be able to account for that scenario.
Thanks
In your application controller create a before filter e.g. before_filter :save_referrer
def save_referrer
unless session['referrer']
session['referrer'] = request.env["HTTP_REFERER"] || 'none'
end
end
When a user buys an item you can save the referrer in a field either in the order table or create a separate table for referrals which i think is the better option, you can store views for each referrer by counter caching that column. You can also use first_or_create if you don't want to have to group by the referrer column and so on, it's up to you. Anyway you get conversion like this;
Conversion = (Sales/Number of unique views for a referrer) * 100
I currently have a rails project that looks to let customers authenticate their account in two separate ways. Either they can use a bar code that they have stored in the database, or they can use a phone number. I want to merge these two fields so that there will be one field that allows two sources of authentication. Here is my code for the two fields.
def User.authenticate(barcode)
if user = find_by_barcode(barcode)
user
end
end
def User.authenticate2(phone_number)
if user = find_by_phone_number(phone_number)
user
end
end
Any suggestions would be greatly appreciated.
I would suggest using this one for simplicity of code:
def User.authenticate(barcode_or_phone_number)
find_by_barcode(barcode_or_phone_number) || find_by_phone_number(barcode_or_phone_number)
end
However it might cause 2 sql to be queried. So you may consider this:
def User.authenticate(barcode_or_phone_number)
where("users.barcode = ? OR users.phone_number = ?", barcode_or_phone_number, barcode_or_phone_number).first
end
I have a feature called "Browse" that allows users to browse through random profiles. When a user clicks on "browse" they are immediately taken to a users profile that they are NOT already friends with. What should my controller look like?
Right now I've got:
def browse
#users = User.all.offset(rand(current_user.matches.count))
#users.each do |user|
if !current_user.friends.include?(user)
#user = user
return
end
end
end
However that doesn't seem to be working. Any advice? I am admittedly bad with blocks, it seems!
You could try something like this
def browse
#user = (User.all - current_user.friends).sample
end
A better version would be
def browse
#user = User.where('id not in (?)', current_user.friends.map(&:id))
.offset(rand(current_user.matches.count)).limit(1)
end
Also, if you are too concerned about performance, instead of using the offset technique, better use the randumb gem to fetch the random record. It uses database specific functions for selecting random records, if available.
Add an extra method to your User, something like this:
def random_stranger
self.class.where(%Q{
id not in (
select friend_id
from friends
where user_id = ?
}, self.id).
order('random()').
limit(1).
first
end
Then in your controller:
def browse
#user = current_user.random_stranger
end
If your database doesn't know how to optimize that not in then you could replace it with a LEFT OUTER JOIN combined with WHERE friend_id is null.