I am following the Treehouse tutorials on creating Rails App. My GitHub folder is: https://github.com/phanatuan/facebook
I have the User & the Statuses object, where I have set up the Associations for the two as follow:
Status belongs_to User
User has_many Statuses
However, I could not call the method #status.user.email to display the User Email in my views\statuses\show.html.erb.
Related
hi currently i am making a new project using rails 5, and devise , my problem is that i am trying to get the current user that is logged into my webapp, and then get the current users id and save it into the database.
currently getting a lot of error and having trouble doing so.
my database are the following for the sample
Note this is the default devise setup for the user
User
username
password
email address
blog
title.text
body.text
user_id.integer
now my problem is that how do i get the current user that is logged into my webapp, and then save it into the blog database that contains the user_id. i havent added the blog yet into one of my samples , but i am currently failing at the results that i need to be making and pushing forward into it.
is there a way to scaffold it , or do i need to go commando and hardcode it, if so how is it possible to do so?
get the current user so that it gets the current user id and save it to each blog.
2.how do i push it into the controller
how do i do it into the model as well
i am a bit confused as to how it should be made and done , with rails any help will be appreciated
In your user.rb model
define relationship with user in this way
has_many :blogs
in your bolg.rb model
belongs_to :user
now in your create method in your blog controller
#user = current_user
then
if #user.blogs.create(blog_params)
#your logic
end
This automatically save user_id to database.
I have a rails 4.1 application setup with Devise.
I have:
one USER model and one PROFILE model. The user has_one profile
However, I have two types of users - Buyers and Sellers (it's a service platform and I have to use states to check if the user & profile is 100% completed to mark a user active).
Everything is working up to this level, but I am looking for some suggestion on how to model these two user types. Please note that the sellers have more responsibility, like adding portfolio items, applying for jobs etc. The buyers on other hands only post jobs and message sellers. Think of it like any other standard marketplace.
My question is:
Whether to use STI, please if someone can let me know how to do this, using the current setup?
Use roles system using Cancan or other?
The current setup is:
User model:
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy, autosave: true
class Profile < ActiveRecord::Base
belongs_to :user
All profile data of both users types is in profiles table.
Thank you for your time.
I am using Rails 4.0 and would like to use Devise for my sign-up/sign-out/etc.
However, on my sign-up page I would like to have fields not only from the user model that devise creates but also another model that users have a relationship with (organizations in this case).
I have setup associations on the models for user and organization like so:
user.rb
has_one :organization
accepts_nested_attributes_for :organization
organization.rb
belongs_to :user
Any ideas on how to make this happen? I tried overriding the registration controller for devise but haven't had any luck.
You need to override the views and insert a fields_for block for that :organization association. Run rails generate devise:views and look for the new view files in your app/views/devise directory.
I'm using rails 3.1 with Devise for user authentication...
I'm having trouble either getting the associations between two scaffold generated models working or I'm not accessing it correctly.
I have two models: User (Devise), and Post
I've added the belongs_to :user tag in the Post model and has_many :posts in the User model.
When I open a rails console, rails c from terminal in the app directory, I thought I should be able to reference the Post model through the user model. Such as User.post.count but I keep getting a NoMethodError: undefined method "post" error.
I've been through all the guide.rails.org and back through the railstutorial.org book and I'm definitely missing something somewhere.
Any help would be much appreciated.
Have you added your migrations that adds user_id to the posts table? Without that foreign key, Rails can't figure out which users are associated with which posts.
I'm a newb with Rails and am trying to get out my first Rails 3 app with Devise. I have a situation where pretty much everything on my site is specific to a user. I'm creating a kind of private gallery that isn't public facing, so that every image belongs_to a user and a user has_many images. Here's my issue... I want to rework my routes so that showing a users images doesn't require a user id in the URL. I will be showing a user's images in the user/show route.
current route (from rake:routes):
/users/show(.:format) {:action=>"show", :controller=>"users"}
Is it possible to have devise use only "resource" instead of "resources"? so it would be /users/show/? Am I making sense? I am not sure the terminology to ask, but I want all of a user's functionality to imply that I know who the user is, then I can check that in the controllers.
Thanks!
In your controller, for the show action:
def show
#user = current_user
end
That's all you need to do. If you were EXPECTING an id, then you do something like #user = User.find(params[:id], but since you know what user you want (in this case, the current_user, which is exposed by Devise), you don't need to do anything special.