How do I show only Things with Username of "abc"? - ruby-on-rails

I have a Thing model:
class Thing < ActiveRecord::Base
belongs_to :user
has_attached_file :image, :styles => { :medium => '300x300>', :thumb => '100x100>' }
validates :image, presence: true
validates :description, presence: true
end
I also have a User model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :things
validates :name, presence: true
validates :username, presence: true
validates :email, presence: true
end
My UsersController looks like this:
class UsersController < ApplicationController
def show
#user = User.find_by_username(params[:username])
end
end
When I navigate to localhost:3000/users/Delacram, where Delacram is the username, how do I display only the Things with a username of Delacram?

Given you have the #user that has many things:
#user.things
You can loop on it:
#user.things.each do |thing|
= thing.description
...

also... in the controller, you can call an instance of #things = #user.things. Then in your view, you can call = render #things. This will look to views/things/_thing.html.haml, and plop whatever's in that file onto the page for each instance of #thing.
Rails magic...

Related

when seller in the dashbard it can create new product. when buyer comes i only want to show index and show

Here is my route.rb
Rails.application.routes.draw do
resources :products
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users, controllers: { registrations: 'users/registrations'}#, confirmations: 'users/confirmations' }
root 'home#homepage'
get 'dashboard', to: 'home#dashboard'
end
Here is the product controller
class ProductsController < InheritedResources::Base
private
def product_params
params.require(:product).permit(:title, :description, :price, images: [])
end
end
Here is the product model
class Product < ApplicationRecord
has_many_attached :images
validates :title, presence: true
validates :description, presence: true
validates :price, presence: true
validate :image_type
private
def image_type
if images.attached? ==false
errors.add(:images, " are missing")
end
end
end
Here is the user model
class User < ApplicationRecord
validates :email, format: { with: /\A[\w+\-.]+#[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i, message: "Email invalid" },
uniqueness: { case_sensitive: false },length: { minimum: 4, maximum: 254 }
has_many :products
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
enum role: { Buyer: 0, Seller: 1 }
devise :registerable, :confirmable
end
In the the views home folder dashboard.
<h1>Dashboard</h1>
<%= link_to "Product", products_path %>
I uses scaffold that seller can perform crud operations and add multiple images..Now when buyer see seller product I want him they see only index and show action

Devise Model does not allow Association

I recently downloaded and installed Devise for Rails 4. I used it on the model "actors"; which have a subset model called "employee"
For the class "Actor"
class Actor < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
include UUIDHelper
has_one :access, autosave: true
has_one :employee, autosave: true
has_many :contact_detail, autosave: true
has_many :file_set
has_many :link_set
mount_uploader :logo, AvatarUploader
validates :name, uniqueness: true
validates_presence_of :name
validates_length_of :description, maximum: 256
def actor_name
actor.name
end
validates :logo,
:file_size => {
:maximum => 25.megabytes.to_i
}
end
For the class "Employee"
class Employee < ActiveRecord::Base
include UUIDHelper
belongs_to :actor
has_one :status
has_many :restdays
has_one :regular_work_period
validates_presence_of :actor
end
I previously had this relation unto a view:
<td><%= employee.actor.name %></td>
Now it spews out an error:
undefined method `name' for nil:NilClass
What did I do wrong? Did Devise do something?
That error indicates this employee object doesn't have actor. employee.actor outputs nil.
Just check your employee object is loaded correctly. It is not a Devise problem.

Undefined method `each' for #<User:

Rails 4.1
Ruby 2.0
Credential.rb
class Credential < ActiveRecord::Base
belongs_to :category
has_many :user
validates :name, :login, :password, presence: true
attr_accessor :encryption_key
attr_encrypted :login, key: :encryption_key
attr_encrypted :password, key: :encryption_key
end
User.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :credentials
def you
"You are <b>#{email}</b>"
end
end
CredentialsController.rb
class CredentialsController < ApplicationController
before_filter :authenticate_user!
def create
#credential = current_user.credentials.new
#credential.encryption_key = session[:master_key]
#credential.update(credential_params)
if #credential.save
redirect_to credential_path(#credential), notice: "Password entry created successfully."
else
render "new"
end
end
The line:
#credential.update(credential_params)
throws an exception
undefined method 'each' for #<User:0x4de4f58>
You need to edit your associations. You have credentials that has_many :user and users that has_many :credentials. The one with the foreign key should be a belongs_to not has_many. If you're attempting to make a many-to-many relationship, then either use has_many_and_belongs_to or a join table. Further, it should be has_many :users and not has_many :user. That should resolve your error.

rails: devise ,cancan, rolify to get role name by user

I can easily to test if a user has certain role by
if user.has_role? :admin
How do I get a user's role name?
Something like
users = User.all
user.each{ |user|
puts user.role or users.role_name ?
}
User model
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me ,:username,:first_name,:last_name
# attr_accessible :title, :body
end
role model
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
attr_accessible :name,:id
scopify
end
you can use
user.roles.first.name

Rails Devise polymorphic

I am using devise and I want create a polymorphic relationship, I added the columns to table users 'usersable_type' and 'usersable_id'
This is my code
Model >> User
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
# attr_accessible :title, :body
attr_accessible :email, :password, :password_confirmation, :remember_me
#usarsable
belongs_to :usersable, :polymorphic => true, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :usersable
end
Model >> Medic
class Medic < ActiveRecord::Base
attr_accessible :license_number, :specialty
has_one :user, as: :usersable, dependent: :destroy
has_and_belongs_to_many :patients
end
Model >> Patient
class Patient < ActiveRecord::Base
belongs_to :socialsecurity
attr_accessible :birthday, :blood_type
has_one :user, as: :usersable, dependent: :destroy
has_many :contacts
has_and_belongs_to_many :medics
end
Override Devise Controller
class RegistrationsController < Devise::RegistrationsController
def new
super
#user.build_usersable # I had problem in this line
end
def create
end
def update
super
end
end
Those are all the models I have for the moment, but I still have the same problem, I dont know how create and save the polymorphic object.
The error still is the same
ERROR: undefined method `build_usersable' for "<#User:"
Is anyone can help me I would be grateful
Regards and thanks in advance
Juli.
Based on the conversation in the comments this is the kind of scheme i think you need.
I am going to use the concept of Aspect, that is a user can have many aspects, the aspect might be a medic, a content detail etc; an Aspect can have only one user.
First you need a UserAspect model which has a user_id and a aspect_type and aspect_id
class UserAspect < ActiveRecord::Base
belongs_to :user
belongs_to :aspect, :polymorphic => true
end
Now your user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
# attr_accessible :title, :body
attr_accessible :email, :password, :password_confirmation, :remember_me
#aspects
has_many :user_aspects
has_many :aspects, :through => :user_aspects
end
And now your medic
class Medic < ActiveRecord::Base
attr_accessible :license_number, :specialty
has_one :user_aspect, as: :aspect, dependent: :destroy
has_one :user, :through => :user_aspect
end
Now you can do things like
user.aspects
medic.user
etc

Resources