I have a rails application built on top of the Devise cancan bootstrap repo.
I am trying to make a page for employee's to log hours. I set up a scaffold, and it generates a view with the form on that scaffold, but my question is how do I link to that page from the menu, and how can I make it so those logged hours are tied to whichever employee is logged in.
My scaffold is
rails g scaffold hours email:string day:date hours:integer
So besides user creation and authentication Devise has added the following
User model (backed by a users table in the database)
current_user method available in your controller code and in your views
You want to use a foreign_key user_id instead of email in all related models, your scaffold generation would be something like this (I've changed the naming slightly from yours, but you get the idea)
rails g scaffold TimeSheet user_id:integer day:date hours_worked:integer minutes_worked:integer
Then you need to update your User model and the newly created TimeSheet model to create the associations between the two models
See rails guides for more information: http://guides.rubyonrails.org/association_basics.html
class User
# ...
has_many :time_sheets
end
class TimeSheet
belongs_to :user
end
You will need to remove the user_id from the scaffold generated views and set it in the controller during create and update actions
def create
#time_sheet = TimeSheet.new(params[:time_sheet])
#time_sheet.user = current_user
# ....
Related
So, I have actually generated the views for devise but recently i want to change it back to another name, how will i?
Initially it was rails g devise 'Writer'...
now, i want rails g devise 'User'
Thanks.
Destroy and then generate the model.
First:
rails destroy devise Writer
then:
rails generate devise User
It would be more easier and recommended then changing.
if you cannot destroy your devise model then one method is to
rename occurrences of user_signed_in?, current_user, user_session
(see devise helpers)
Rename your db table name (migration), modify your tests files, model and route to devise_for :users.
Everything now works fine. I deleted the writer's column from the database and generated another one.
Basic example:
1) I create a new rails project with following instruction:
rails new tut3
2) I generate my first scaffold model costumer
rails generate scaffold costumer name:string
3) I generate my second scaffold model product
rails generate scaffold product item:string costumer_id:integer
4) I run the migration (rake db:migrate) and after starting the server (rails s)
and adding a few costumers (e.g. Mario, Anna etc..) I go to the products page and I expected to get a costumer field with a dropdown table showing the ids of the costumers I've added but I see that I can insert in any id number I wish. Why so? Should the costumer field of the model product just restricted to the costumer IDs that I create in the costumer page? and how can I associate the product costumer field just with the costumer's name that I have created?
Hope my question is clear...))
Use
rails generate scaffold product item:string customer:belongs_to
rails generate scaffold does a lot of job for you, but it can't do each and everything for you.
You will have to do manually set other things for yourself. Starting with routes, you have to set them so that you could use something like customers/1/products or customers/2/products. scaffold won't set these routes for you.
resources :customers do
resources :products
end
When you mentioned customer_id while generating scaffold for products, that means a product belongs_to a customer, and you can check it in the code at app/models/product.rb. But now the question is, how the relation goes from a customer to a product. Can a customer have many products, or a customer can have only one product?
In app/models/customer.rb,
class Customer < ActiveRecord::Base
has_one :product # For having only product per customer
# has_many: products # Note that 's' at the end, this makes a customer have as many as products as possible.
end
Similarly, you need to change the view as well as the controller for both fields, and that is a whole lot of process. For that I recommend you to go through the basics of Rails, how do controllers and view work. After that, the stuff would be pretty much easy for you.
I'm making a business administration Rails app.
Is there any info available on how I can enable admin users to add a new field to a table? Behind the scenes this would generate and run the migration and I guess some metaprogramming to include the field in the index, show and _form.html.rb views..
You can't run migrations like that, but you can solve the problem.
The answer is here
403-dynamic-forms
If you con't have a subscription, get one. Railscasts is gold for any rails developer
Basically you make a table in the database for the fields to a model.
For instance
class Product
has_many :product_fields
end
class ProductFields
belongs_to :product
end
I'm using rails 3.2 and devise 2.1 to create a multi-site CMS
Requirements
Sites based Basecamp subdomains.
Have 3 "user" models. 1. Admin(superuser) 2. Authors(each have their own site on subdomain) & Subscribers(read the sites ).
Authors: registration is normal username/password combo but needs to be approved by admin. their registration form will have subdomain field.
Subscribers: registration happens by invitation email.
need separate login & registration forms
Possible Solutions
I have been searching & found few solutions
3 Separate models in devise:
$ rails generate devise admin
$ rails generate devise author
$ rails generate devise subscriber
but this gives the following error
$ rails generate devise author
/home/gaurish/.rvm/gems/ruby-1.9.3-p286-perf/gems/devise-2.1.2/lib/devise/rails/routes.rb:443:in 'raise_no_devise_method_error!': Admin does not respond to 'devise' method. This usually means you haven't loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb' (RuntimeError)
STI: single table in the database and for each user type create a model
class Admin < User; end
class Author < User; end
class Subscriber < User; end
Here, I am not sure how this would handle different login/registration workflows. example for subscriber I am planning on using devise_invitable for creating invitations. Admin doesn't need to scoped on basis of subdomains unlike authors & subscribers.
Does this seem complicated? I hope I was able to explain well.
You don't need to have three separate models to build this functionality. What you want to look at is the concept of Roles which are applied to one User model.
There is a Gem which provides this capability called Rolify and can be found at https://github.com/EppO/rolify
This would allow you to specify which users are in which Roles and change them as you see fit, all from one existing model.
Once you have Roles attached to the User model, you can override Devise's registration controllers to detect the Role and render different templates etc. You would do this by:
Running rails generate devise:views to unpack the views from the Devise gem into your project
Create your own Registrations controller:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
# Add logic here to detect Role and display different forms
end
def create
super
end
def update
super
end
end
Add the correct settings in your routes.rb file to tell Devise to use your new controller:
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
Admin does not respond to 'devise' method.
This may be cos you're also using the activeadmin gem, or something that uses a module called Admin, which causes a name conflict. Try renaming the model to AdminUser
I'm using the Devise Ruby gem in my Ruby on Rails 3 application. When a logged in user creates one of my models, I want to save their user id as the person who created it. What should I do?
Create a migration file with a column of type integer named user_id, this is where the association will be stored. (see the migration guide for details on how to do this).
in your model, add: belongs_to :user (see the associations guide for more details)
in the controller for your model, add #your_model.user = current_user in the create action. This will do the association you're looking for. (current_user is a method provided by devise that returns the User ActiveRecord for the currently logged in user)
Note: there are more than one way of doing the actual association. I'm suggesting to do it in the controller but it could be done in the model or elsewhere.