I have
class User < ActiveRecord::Base
attr_accessible :email
validates :email,
presence: true
serialize :data, ActiveRecord::Coders::Hstore
end
and
<%= simple_form_for User.new do |f| %>
<%= f.input :email %>
<%= f.input :first_name %>
<%= f.input :zipcode %>
<%= f.button :submit, 'Sign up' %>
<% end %>
Why when I want to Sign up I get an error:
undefined method `zipcode' for #<User:0x007fd397631650>
Full trace:
https://gist.github.com/3c9df05758ea3d486989
simple_form can only create inputs for attributes of a model, and it looks like zipcode is not an attribute of your User model.
You should run a migration to add this column to your Users table, and then you will be able to store the zipcode of a user.
Related
I have two account types with the relationship below laid out in models below. I need to build a registration form for #account that has a form select field where a user can select to register for either a student or partner account and for this account record to save according to the selection (i.e. to the students or partners table and the accounts table).
I'm running into issues on the controller Accounts#new method and I'm not sure how to set this up in a way that works.
Account:
class Account < ApplicationRecord
belongs_to :acct_holderable, :polymorphic => true
Student:
class Student < ApplicationRecord
has_one :account, :as => :acct_holderable
Partners:
class Partner < ApplicationRecord
has_one :account, :as => :acct_holderable
View for Accounts#new
<%= form_for(#account) do |f| %>
<%= render 'shared/error_messages', object: #account %>
<%= f.label :account_type %>
<%= f.select :acct_holderable, options_for_select(account_type, #account.account_holderable_type), class: 'form-control' %>
<%= f.label :first_name %>
<%= f.text_field :first_name, class: 'form-control' %>
<%= f.label :last_name %>
<%= f.text_field :last_name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>
Account Helper (for options_select)
def account_type
input =<<-OPTIONS
Student,
Partner,
Other Account TBU,
Other Account TBU
input.split(',')
end
Accounts controller
def new
#account = Account.new
end
def create
#account = Account.new(account_params)
if #account.save
#account.send_activation_email
flash[:info] = "Please check your email to activate your account before logging in!"
redirect_to login_url
else
render 'new'
end
end
Pls check the following link for more info http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
https://6ftdan.com/allyourdev/2015/02/10/rails-polymorphic-models/
These links help you to get better understand about polymorphic association.
class Customer < ApplicationRecord
attr_accessor :date
validates_presence_of :name, :principalAmount,:interestRate,:accountType,:duration,:date
end
Except date all are there in my customers table. I am using simple_form for getting those values. But problem is that there is no validation happening for :date.For others it displays message if the field is empty. How do I display presence_of validation message in simple_form for :date. I am using bootstrap datepicker.
<%= simple_form_for #customer do |f| %>
<%= f.input :name,:autocomplete => :off %>
<%= f.input :principalAmount,:autocomplete => :off %>
<%= f.input :interestRate %>
<%= f.input :accountType %>
<%= f.input :duration,:autocomplete => :off %>
<%= f.text_field :date, "data-provide" => 'datepicker',"data-date-format"=>"dd-mm-yyyy" %>
<%= f.button :submit %>
<% end %>
Edit: When I change f.text_field to f.input it shows the validation message. But it is no more a date picker.
Please I am new to Ruby on Rails and also new to stackoverflow. I am using the simple_form gem to create a Contact form where visitors will sign up for newsletter.
I have a Contact model and new.html.erb view file. The problem is I get the following error mesage when I navigate to the Contact link
"undefined local variable or method `simple' for #<#:0xb44fae84> " Please what am I doing wrong?
Note: I am using Rails 4.2.6 and ruby 2.2.1
Thank you.
The Contact Model is in app/models/contact.rb as shown below.
class Contact < ActiveRecord::Base
has_no_table
column :name, :string
column :email, :string
column :content, :string
validates_presence_of :name
validates_presence_of :email
validates_presence_of :content
validates_format_of :email,
:with => /\A[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
validates_length_of :content, :maximum => 500
end
and the view file in app/views/contacts/new.html.erb is as shown below:
<% content_for :title do %>Contact<% end %>
<h3>Contact</h3>
<div class="form">
<%= simple_form_for #contact do |form| %>
<%= simple.error_notification %>
<%= form.input :name, autofocus: true %>
<%= form.input :email %>
<%= form.input :content, as: :text %>
<%= form.button :submit, 'Submit', class: 'submit' %>
<% end %>
</div>
simple isn't a defined method, like the error says.
Use Simple Form's error notification method in your form:
= f.error_notification
Or, for better customization, add your own helper partial to render errors (I'm using the slim templating engine in this example):
_form_errors.html.slim:
- if resource.errors.any?
#error_explanation
.alert.alert-error
h2.error-header Please fix the following #{resource.errors.count} errors
ul
- resource.errors.full_messages.each do |msg|
li= "* #{msg}"
In your form
= render "partials/form_error", resource: #contact
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], ...
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!)