How do I solve this Devise authentication challenge? - ruby-on-rails

Hello people I have some challenges here with devise authentication for users:
I have two models called color and sub_color in my application. the sub_color belongs_to color and color has_many sub_colors.I have already seeded the database with the appropriate data
The challenge; I want a user to be able to chose these in the devise form_for when they are registering as a collection object and the id of the sub_color will be used for identifying a particular user also(a situation where for instance I can sort all the users which cosed blue color). How do I achieve this please?
This is what I have tried but it is not working:
%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :user_name, required: true %>
<%= f.input :password, required: true, hint: ("# {#minimum_password_length} characters minimum" if #minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
<%= f.input :first_name, required: true %>
<%= f.label :color_id, "Color" %> <br/>
<%= f.collection_select :color_id, Color.order(:name), :id, :name, include_blank: true%>
<%= f.label :sub_color_id, "Sub Color" %> <br/>
<%= f.grouped_collection_select :sub_color_id, Color.order(:name), :sub_color, :name, :id, :name, include_blank: true%>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
model for users:
belongs_to :sub_color
has_one :color, through: :sub_color
devise.......
end
model for sub_color
has_many :users
belongs_to :color
end
model for color
has_many :sub_color
end
This is the error I see on the web browser
NoMethodError in Devise::Registrations#new
[undefined method `color_id' for #<User:0xbacc720>]

Firstly, You need to add color_id and sub_color_id to users table.
Then define association, in user.rb belongs_to :sub_color and in sub_color.rb has_many :users. Same goes with color, user.rb belongs_to :color and in color.rb has_many :users .
Hope that helps!

Related

Rails validations throwing an undefined method error map with simple_form

I'm trying to validate my form inputs in the course model however the second I try to validate :presence to true with any form input I receive the below error. Something to do with the map collection.
undefined method `map' for nil:NilClass
The error is throwing up this line of code in my form. It's to do with categories map. If I have no validations and create my course leaving everything blank it created the course. I want to include presence true for everything so I've added true to all the data points.
validates :course_reference, :title, :course_img_file_name, :category_id, :description, :short_description, :venue, :location_id, :duration_days, :duration_weeks, :start_date, :start_time, :end_date, :max_enrolment, :price, :presence => true
Error occurred. I've reduced it to trying each form data on its own however the second I have any validates in the model I get the map error.
Can anyone help please.
_form.html.erb
<div class="row">
<div class="site-forms">
<div class="col-md-10">
<%= simple_form_for #course do |f| %>
<%= f.input :course_reference, placeholder: "Course Reference", required: true, label: false %>
<!-- <= f.input :course_img, as: :file, required: true, label: "Please upload a brand image for your course" %><br> -->
<span class="btn btn-default btn-file">
<i class="fa fa-cloud-upload fa-lg"></i> Upload Image
<%= f.input :course_img, as: :file, required: true, label: false %>
</span> Please keep images to 225hx300w for best display settings <br><br>
<%= f.input :title, placeholder: "Course Title", required: true, label: "Course Title" %>
<%= select_tag(:category_id, options_for_select(#categories), :prompt => "Select Category") %><br><br>
<%= f.input :description, as: :wysihtml5, placeholder: "**** NOTE DO NOT USE HEADER 1,2 or 3 TAGS. THIS WILL NOT HELP SEO FOR YOUR COURSE ***", required: true, label: "Description (Please be descriptive about the courses content)" %>
<%= f.input :short_description, placeholder: "Please input a short description for this course", required: true %>
<%= f.input :venue, placeholder: "Venue full address", required: true, label: false %>
<%= select_tag(:location_id, options_for_select(#locations), :prompt => "County") %><br><br>
<%= f.input :duration_days, placeholder: "Total amount of days the course is run. Enter 0.5 if course runs for a half day. " , required: true, label: false %>
<%= f.input :duration_weeks, placeholder: "How many weeks does the course run", required: true %>
<%= f.input :start_date, required: true %>
<%= f.input :start_time, required: true %>
<%= f.input :end_date, required: true %>
<%= f.input :max_enrolment, placeholder: "Course capacity", required: true %>
<%= f.input :price, placeholder: "EUR", required: true %>
<%= f.button :submit, class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
Course.rb
class Course < ActiveRecord::Base
validates :course_reference, :presence => true
searchkick
belongs_to :user
belongs_to :category
belongs_to :location
has_many :subscriptions, dependent: :destroy
has_many :comments, dependent: :destroy
geocoded_by :venue
after_validation :geocode, if: :venue_changed?
has_attached_file :course_img, styles: { course_index: "300x300>", course_show: "400x600>", course_search: "100x100" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :course_img, content_type: /\Aimage\/.*\z/
def price_in_cents
price*100
end
end
Found the answer after a bit more searching. Sorry lads.
Ok so it's to do with the create action in my controller not having the variable #categories in it. Throws up the error then. Found answer on this thread.
Rails undefined method `map' for nil:NilClass - form collection_select

Simple Form Gem - How to display the name of a model in an association - Rails 4

I'm new to rails & I know this may be a very simple question, but I'm unsure how to display the name of a company using 'simple form' association?
In my schema I've a table companies with the columns name & content
In my schema I also have a table users with the columns first name, last name & company_id:integer
A company has_many :users
A user belongs_to :company
Everything works perfectly in my views expect the display of the name of my company
in my views:
<h2>Sign up Primary Admin</h2>
<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.association :company, collection: Company.all.order(:name), prompt: "please select your company", label: 'Company' %>
<%= f.input :firstname, required: true, autofocus: true %>
<%= f.input :lastname, required: true, autofocus: true %>
<%= f.input :email, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
i get the below display:
label_method => the label method to be applied to the collection to
retrieve the label (use this instead of the text_method option in
collection_select)
You should define the label_method to name to display the company names.
<%= f.association :company, collection: Company.all.order(:name), prompt: "please select your company", label_method: :name, label: 'Company' %>
SimpleForm uses to_s method for association labels, so you have to define your own to_s method for your company model.
i. e:
class Company < ActiveRecord::Base
def to_s
name
end
end

Rails foreign key is is allways null after saving

I have two models,
class ProcessType < ActiveRecord::Base
has_many :remarks
validates :code, :name, presence: true
end
class Remark < ActiveRecord::Base
belongs_to :process_type
belongs_to :origin
validates :description, presence: true
end
The foreign key is set correctly, the remarks table has a process_type_id column.
On the Create form, I display the processes available using a select:
<%= simple_form_for #remark do |f| %>
<%= collection_select(:remark, :process_type_id, ProcessType.all, :id, :name) %>
<%= f.input :description, label: 'Description' %>
<%= f.input :suggestion, label: 'Suggestion' %>
<%= f.button :submit %>
<% end %>
My problem is that in the remarks table, after saving, the id of the process is always null. What am I missing? Is probably something obvious, but I am not able to see it right now.
Thanks!
Try this:
<%= f.collection_select :process_type_id, ProcessType.all, :id, :name %>
or, if you want to use the Simple Form methods:
<%= f.input :process_type_id, :collection => ProcessType.all %>

How to build a devise nested resource during registration?

During registration of a new user with Devise, I need to create a new Family object link to this new user at the same time (the user being the head of the family).
My family model:
belongs_to user
My user model:
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :family
has_one :family
accepts_nested_attributes_for :family
In devise/registration/new.html.erb
<%= simple_form_for([resource, Family.new], :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form-vertical' }) do |f| %>
<%= f.error_notification %>
<%= display_base_errors resource %>
<%= f.input :name, :autofocus => true %>
<%= f.input :email, :required => true %>
<%= f.input :password, :required => true %>
<%= f.input :password_confirmation, :required => true %>
<% f.fields_for :family do |family_form| %>
<p><%= family_form.label :name %></p>
<p><%= family_form.text_field :name %></p>
<% end %>
<%= f.button :submit, 'OK', :class => 'btn-primary' %>
<% end %>
But this is not working, I find a couple of question like this but I did not manage to fix that.
Any idea ?
UPDATE 1
I got the following error:
undefined method `email' for #<Family:0x007f892a12c310>
Family is a model that do not have any email, just a name. I just need to be able to create a new Family object with a name when creating a new user (and link it to the user as well).
UPDATE 2
I have added resource.build_family in my registrations controller. The family object is correctly created and associated to the user (I can display <%= resource.family %> in new.html.erb for debugging), but still no form displayed for the family.
You need the equal sign in the <%=fields_for
<%= f.fields_for :family do |family_form| %>
<p><%= family_form.label :name %></p>
<p><%= family_form.text_field :name %></p>
<% end %>
And in your user model you need to make the :family_attribues accessible and not :family
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :family_attributes
has_one :family
accepts_nested_attributes_for :family
If you're getting undefined method 'email' for #<Model:0x007f892a12c310>:
You need to overwrite Devise::RegistrationsController as described in the docs: https://github.com/heartcombo/devise#configuring-controllers. E.g.
class Users::RegistrationsController < Devise::RegistrationsController
def new
super do |resource|
resource.build_<model>
end
end
end
And you must only specify resource in form_for: simple_form_for(resource, ... instead of simple_form_for([resource, Model.new], ...

How do I make nested_attributes work in Rails3?

Here's my user model:
class User < ActiveRecord::Base
has_one :teacher, :dependent => :destroy
accepts_nested_attributes_for :teacher, :allow_destroy => true
attr_accessible :email, :password, :password_confirmation, :remember_me, :teacher_attributes
end
Here's my teacher model:
class Teacher < ActiveRecord::Base
belongs_to :user
attr_accessible :user_id, :first_name, :last_name
validates_presence_of :user_id, :first_name, :last_name
end
Here's my form:
<%= form_for(#user, :url => registration_path(:user)) do |user| %>
<%= user.text_field :email %>
<%= user.text_field :password %>
<%= user.text_field :password_confirmation %>
<%= user.fields_for resource.build_teacher do |t| %>
<%= t.text_field :first_name %>
<%= t.text_field :last_name %>
<%= t.text_field :phone %>
<% end %>
<%= user.submit 'Confirm' %>
<% end %>
Except that this thing won't "accept nested attributes"
My development log says:
WARNING: Can't mass-assign protected attributes: teacher
I don't know if it's related, but the form isn't generating fields inside a teacher_attributes array or anything - it's inside teacher. I'm guessing that's where my problem is, but I don't how to make it put the fields inside it. Please help.
Thanks!
Try these things:
At top of view:
<% #user.build_teacher if #user.teacher.nil? %>
For the fields for:
<%= user.fields_for :teacher do |t| %>
Also, personally, I like naming the block parameters in forms (the part |user| and |t|) as |form| (because when you're having a long day, and you see user down in the view and not form, it can confuse you!)

Resources