Rails ActionView form_for failure - ruby-on-rails

I just added a new column to my database. The migration didn't throw any errors and the database looks like it took the migration just fine also. I have a form, as such;
<h1>Sign up as a new user</h1>
<% #user.password = #user.password_confirmation = nil %>
<%= error_messages_for :user %>
<% form_for(#user) do |f| -%>
<p><%= f.label :login %><br/>
<%= f.text_field :login %></p>
<p><%= f.fullname :fullname %><br/>
<%= f.text_field :fullname %></p>
<p><%= f.label :email %><br/>
<%= f.text_field :email %></p>
<p><%= f.label :password %><br/>
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation, 'Confirm Password' %><br/>
<%= f.password_field :password_confirmation %></p>
<p><%= submit_tag 'Sign up' %></p>
<% end -%>
The field f.fullname was the new column I added. When I try to load up the page though, it keeps throwing an error;
undefined method `fullname' for #<ActionView::Helpers::FormBuilder:0xb6fa73e4>
I have this in my user.rb model
attr_accessible :login, :email, :fullname, :password, :password_confirmation
Am I missing something here as to why Rails keeps throwing that error?
Thanks.

It looks like the error is coming from a typo in your view:
<p><%= f.fullname :fullname %><br/>
<%= f.text_field :fullname %></p>
f.fullname should be f.label.
A clue would be that the error is coming from FormBuilder and not an ActiveRecord derivative.

Related

Why am I getting undefined variable for my params? Fields_for multiple model params

I am getting an undefined variable error with my app, claiming that my params is an undefined variable. It is suggesting that I use participant_path instead?
I am working on an app that has multiple models. There is a form that is using fields_for to fill in data for the a model which is hanging off the primary model.
When testing it I am using the primary model’s controller to process the form submission and defining the params to include both models’ attributes.
Is this the correct way to use fields_for?
I will add my code later, as I am posting this from my phone.
UPDATE with code:
Here is my form:
<%= form_for #participant, url: {action: "create"} do |f| %>
<%= f.label :first_name, 'First:' %>
<%= f.text_field :first_name %>
<%= f.label :last_name, 'Last:' %>
<%= f.text_field :last_name %>
<%= f.label :gender, 'Sex:' %>
<%= f.select :gender, ['Male', 'Female'] %>
<br>
<br>
<%= f.label :email, 'Email:' %>
<%= f.text_field :email %>
<%= f.label :phone, 'Phone:' %>
<%= f.text_field :phone %>
<%= f.label :street_name, 'Street Number & Name:' %>
<%= f.text_field :street_name %>
<%= f.label :city, 'City:' %>
<%= f.text_field :city %>
<%= f.label :state, 'State:' %>
<%= f.text_field :state %>
<%= f.label :zip, 'Zip:' %>
<%= f.text_field :zip %>
<%= f.hidden_field :role, :value => 'Reader' %>
<%= f.fields_for #participant.student_details do |student_detail_field| %>
<%= f.label :nationality, 'Nationality:' %>
<%= student_detail_field.text_field :nationality %>
<%= f.label :religion, 'Religion:' %>
<%= student_detail_field.text_field :religion %>
<%= f.label :birthdate, 'Birthdate:' %>
<%= student_detail_field.date_field :birthdate %>
<%= f.label :need_ride, 'Do you need help getting to the building?' %>
<%= student_detail_field.select :need_ride, ['Yes','No'] %>
<br>
<br>
<%= f.label :has_spouse, 'Marital Status:' %>
<%= student_detail_field.select :has_spouse, ['married', 'single'] %>
<br>
<br>
<%= f.label :spouse_name, "If married, spouse's name:" %>
<%= student_detail_field.text_field :spouse_name %>
<%= f.label :english_level, 'English Level:' %>
<%= student_detail_field.select :english_level, ['Low', 'Medium Low', 'Medium', 'Medium High', 'High'] %>
<%= f.label :expectations, 'What are your expectations for the program?:' %>
<%= student_detail_field.text_area :expectations %>
<%= f.label :length_of_stay, 'About how long will you be in Nashville?:' %>
<%= student_detail_field.select :length_of_stay, ['Less than 1 Year', '1 Year', '1-2 Years', '2-3 Years', '3+ Years'] %>
<%= f.label :exact_length, 'Please tell us exactly how long you plan to be in town:' %>
<%= student_detail_field.text_area :exact_length %>
<% end %>
<br>
<br>
<%= f.submit 'Register' %>
<% end %>
Here is the relevant code in my controller:
def new
#participant= Participant.new
end
def create
#participant = Participant.create(participant_params)
#participant.save
if #participant.save
flash[:success] = "Successfully Registered!"
#email notifes admin of new registration
NotifyMailer.notify_email(#participant).deliver_now
redirect_to '/signup'
end
def participant_params
params.require(:participant).permit(:first_name, :last_name, :gender, :email, :birthdate, :phone, :street_name, :city, :state, :zip, :nationality, :religion, :need_ride,
:has_spouse, :spouse_name, :english_level, :expectations, :length_of_stay, :exact_length, :volunteer_id, :matched, :returned_home)
end
When I test this on the local server, I get this error:
undefined local variable or method `participant_params' Did you mean?
participant_path participant_url participants_path
Thanks in advance!

ActiveRecord::PendingMigrationError in rails

undefined method `gender' for #
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name %>
<%= f.text_field :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, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.label :gender %>
<%= f.text_field :gender, class: 'form-control' %>
so I add gender attribute to users db
rails generate migration AddGenderToUsers gender:string
rake db:migrate
rails server
Then all pages are not accessible .
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
I don't know why as it worked before .
I tried to rake db:migrate ,but it didnt work .

Rendering devise error messages in scoped custom form

I have to Devise models, User and Vendor. I generated the views for Vendor and customized the signup form to suit my needs. I needed to tweak the registrations controller for Vendor, like so:
controllers/vendors/registrations_controller.rb
class Vendors::RegistrationsController < Devise::RegistrationsController
skip_before_filter :require_no_authentication
def create
super
end
protected
def sign_up(resource_name, resource)
true
end
def new
super
end
end
My signup view for Vendor looks like so:
views/vendors/registrations/new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:multiparet => :true} ) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :store_name %>
<%= f.text_field :store_name %></div>
<div><%= f.label :contact_name %>
<%= f.text_field :contact_name%></div>
<div><%= f.label :contact_phone %>
<%= f.text_field :contact_phone %></div>
<div><%= f.label :address %>
<%= f.text_field :address %></div>
<div><%= f.label :image %>
<%= f.file_field :image %></div>
<div><%= f.label :email %>
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "vendors/shared/links" %>
When I attempt to render the page, I get this error:
undefined method `errors' for nil:NilClass
How can I render devise error messages in this situation? Thanks in advance!
I would write this as a comment, but it will be easier to read as an answer:
Perhaps you could try replacing devise_error_messages! with a standard Rails error display:
<% if resource.errors.any? %>
<div class="error">
<strong><%= pluralize(resource.errors.count, "Error") %></strong>
<ul>
<% resource.errors.each do |attr,msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

How to customize Devise_invitable generated forms?

The form path is user/invitations/invitations/edit.html.erb and new.html.erb
Edit.html.erb
<h2><%= t 'devise.invitations.edit.header' %></h2>
<%= form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => { :method => :put } do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :invitation_token %>
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit t("devise.invitations.edit.submit_button") %></p>
<% end %>
I added the following by myself in the edit.html.erb but the form isn't showing up the fields
<p><%= f.label :username %><br />
<%= f.text_field :username %></p>
Please let me know how to deal with this. And customize forms for devise_invitable
I Generated views again by following command:
rails generate devise_invitable:views users
and then found that the nesting done users/invitations/invitaions/edit.html.erb was wrong.
It should be like views/users/invitations/edit.html.erb
This solved my problem and now i am able to customize devise_invitable form.
Your replacement form should go here
app/views/devise/invitations/edit.html.erb

Devise... additional columns to User model not validating

I am using Devise for authentication in a Rails 3 app. I added first_name and last_name columns to the database via db:migrate. The registration form was modified like so:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :first_name %><br />
<%= f.text_field :first_name %></p>
<p><%= f.label :last_name %><br />
<%= f.text_field :last_name %></p>
<p><%= f.label :email %><br />
<%= f.text_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
Validation in the User model:
validates :first_name, :last_name, :email, :password, :password_confirmation, :presence => true
For some reason I am getting "First name can't be blank" and "Last name can't be blank" validation errors. It is interesting to note that these are the two fields added in addition to the stock Devise fields. Does Devise prevent adding other fields to the User model? What other reason could there be?
Thanks.
Probably your user model has defined attr_accessible. if this is true then you have to add :first_name and :last_name to the list.

Resources