I guess not much help here. When you are trying to associate a user from a dropdown list, and imagine if you have 1000000 users, you would see the user instance. I need it to show the actual user's email address.
app/models/user.rb:
has_one :company
app/models/company.rb:
belongs_to :user
The link I was given has nothing to do with the dropdown's value.
Have I setup my rails association incorrectly? Funny thing was, using rails admin I had no issue in this department as I could associate a company when creating a user but not so with active admin.
All I want is when I select the User dropdown, as in picture, I'd see a list of user email addresses.
Tim was correct all this time. I needed to create a function in the User model.
app/models/user.rb:
def display_name
email
end
Related
Rails newbie: I currently have a basic app where Customers(users) have many points (customer model and points model). And I want an admin user (new model) to have the ability to add points to the customer.
-The customer enters their phone number (#index route).
-If the customer is not found, they will be brought to a signup page (#new/#create route).
-If customer is found in the database, their profile will show (#show route).
Now on this page, I want to be able to have an admin passcode, which once entered, gives access to adding points. I also want to keep track of which admin user, gives which customer points on a different page.
How would the schema look like with the admin user? How would I give it access certain access to features like adding points. (I assume I'm going to have to create a helper method for checking if admin is logged in, and keep track of that somehow, maybe with sessions?)
class Customer < ActiveRecord::Base
has_many :points
end
class Point < ActiveRecord::Base
belongs_to :customer
end
class Admin < ActiveRecord::Base
#??? (my best guess is has_many :points, has_many :customers)
end
www.loyalty-app.herokuapp.com
I think the best way is to not have a special admin class and just make admins a type of customer (or user). Then use a gem like cancan or access granted (I prefer access granted) to handle what the different types of users can do.
I'm creating a app of which a unique user registers people entering a building.
The user takes the person's name, ID, plus a picture. When a person is registered,it updates a feed (like twitter's feed) that shows who entered the place (showing only the name and id). Each addition to the feed is actually a data model called "micropost". So each user has many microposts, and "micropost" belongs to "user".
The feed has to show all people who entered the building at a given day, both newly registered visitors and visitors registered long ago who just happened to pass by.
The issue is: i need that when the user clicks on visitor's name in the feed, it gets redirected to a profile page of that particular visitor, showing his/her complete info (name,ID and photo), plus the dates of all prior visits. I need to have many microposts redirecting to the same visitor profile, if they are showing information about the same person.
I'm trying to figure out the model relation between the user, microposts and visitors profiles. I don't know how it would work in this case.
The relation between user and micropost is easy to get, because users are unique. The problem is that microposts aren't, and as just said, even already registered visitors must be shown in the feed.
Thanks for any constructive feedback.
i think you are facing issue with microposts,visitors history
create table visitors(user_id,post_id, created_at)
1) When user visit post, insert a new record in the visitors table with the id of post, fetch record from visitor table to display previous user history.
in your models:
class User < ActiveRecord::Base
has_many :microposts
end
class Micropost < ActiveRecord::Base
belongs_to :user
end
in controller:
#user = User.find(:id)
in view:
<p>#user.name</p>
#user.microspots.order(:created_at).each do |mp|
<p>IN:<%=mp.created_at%></p>
end
I hope this help you.
Using devise, what is the best way to have multiple models or roles?
Here are the models or roles I need in my app:
-Author: can upload content to the site
-Customer: pays a monthly fee to access the content
A user can be both an Author and a Customer. I think they can share the same login form because they will both log in with their email address.
I have tried using CanCan and rolify, but couldn't figure out how to add different roles during registration. When a user registers as an Author, he should be given the "author" role and when a user registers by paying the monthly fee, he should be given the "customer" role. I don't want to use a checkbox for the roles either.
There is a nav link called "Authors" that will allow authors to register and then there is a link for users to register by going through the payment and billing form. Based on which link is clicked, should determine which role is given. Should I pass in a role parameter in the url during registration like "user/sign_up/index?role=customer"? If so, how do I get devise to use this in the registration process?
Using devise, CanCan, and rolify, how can I solve this? I thought it might be better to have a User class and then have Customer and Author extend from User, but from reading many similar questions on StackOverflow it seems rolify is easier.
I know what I am trying to do is very basic, I just haven't figured it out yet. I have been trying to find a solution for this all day.
Update:
Danny's answer below pushed me in the right direction and I have managed to get the roles added properly during registration by passing in a parameter in the URL "users/sign_up?role=customer" or "users/sign_up?role=author". I don't know if this is the best way, but that's what I have so far.
One question I have though is how will my has_many relations work now? Here is what I have:
class User < ActiveRecord::Base
rolify
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :plan
has_many :files
end
has_one :plan is for the customer's subscription plan
and has_many :files
is for the author who can upload many files that the customer can
download
The problem is that I would also like to keep track of the user's file downloads so I would like to put has_many :downloads or probably has_many :files, :through => :downloads
Does it matter if I put these in the User model knowing that an author who is not a customer is not allowed to download files and that a customer who is not an author cannot upload files?
From your question, I understand you've already "dived" into rolify and cancan, so I'll focus on assigning roles.
You typically create different roles in your seeds.rb file, with something like
[:admin, :author, :customer].each do |role|
Role.find_or_create_by_name({ name: role }, without_protection: true)
end
You assign the :admin role immediately when you create yourself as administrator, also in the seeds.rb file, using
user.add_role :admin
For your other roles, you assign them "when it's appropriate", exactly as you describe. When a user clicks the Authors link, and proceeds, this triggers some action in some controller. It is there that you assign this role to that user, by using the same
user.add_role :author
You can also assign roles, connected to certain objects only. E.g. an author is author for only the documents he creates himself. In that case you don't want to assign a "general" author role, but you will assign it like
user.add_role :author, document
in the controller's create action, together with saving the created document. Alternative for this kind of assignment is to do it in a callback from your model.
More questions? Just ask!
class Company < ActiveRecord::Base
has_many :users
end
The user hits on Company->Users href link and assuming it shows 3 users for a certain company.
I would want to set user.age (an integer) for all the users and press save. How do I do this in my controller / view code?
We usually have an Edit link against each User to modify its details, but I would like to set age information for all users.
Use a form_tag to submit to an arbitrary controller action set up specifically for editing multiple users (not one of your user scaffold methods). Then, add one fields_for tag per user inside of the form you created, each with a field for the age attribute. When submitted, you can simply iterate over the user params in the controller to manipulate your users.
I am building an ecommerce app in rails and would like some help on my model relationships and how i get the tables populated with the information.
I have a user model, an order model and an address model. I want to capture the address in a form during the order process.
In my order model i have user_id, pay_type, email, address_id and name.
In my address I have the user_id, (then some address fields).
the relationships are
order belongs_to a user
address belongs_to a user
address belongs_to an order
order has_one address
user has_many orders
user has_one address
So when a customer clicks checkout I want them to enter the name thats on the Order, email, address and paytype. The user_id will be captured using currently signed in user method through a hidden field. (If there is a better way let me know). sorry if this isn't explained very well
You'll need to look into accepts_nested_attributes and nesting forms.
Or just look through some code here, e.g.
order model
order form
order controller
The main things here to note are:
accepts_nested_attributes_for
f.fields_for :address do |address| will be needed to render the nested form
#order.save in the controller will save all nested data