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 .
Related
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!
I want to add custom field tag in devise registration view but that field doesn't exist in users table. I just want user select some value in that field and i get information about selected value in params at the time of sign up. Can anyone suggest me how to do this?
Devise_registration_view:
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :type, 'Role' %>
<%= f.select :type, %w{Admin Teacher Student Guardian}, :prompt => 'Select', required: true, placeholder: 'Roles' %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
You can use attr_accessor for User model which will help you to get and set value for attribute which is not there in table, Please refer this link. attr_accessor_details
updating answer for adding custom validation for attribute created by attr_accessor
attr_accessor :test_attribute
validate :check_test_attribute
def check_test_attribute
errors.add(:test_attribute, "is missing") if test_attribute.blank?
end
I have an attribute called discount_info. I would like to create another field in the devise edit form, so user can edit this information. I would like to have the field be a text_area. I'm not sure how to do this in rails.
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.email_field :address, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<%= f.text_area :discount_info, :class => "", :placeholder => "if you want a placeholder" %>
Just add the attribute in the form and permit it in the configure_permitted_parameters method.
<div class="field">
<%= f.label :discount_info %><br />
<%= f.text_area :discount_info %>
</div>
And in the controller
def configure_permitted_parameters
#other code
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:other_attributes, :discount_info) }
end
I want the user to be able to register in two steps as I have many fields. Ideally first step would be to accept email and password. As user enter it, they can proceed to fill the next step.
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :first_name %>
<%= f.text_field :first_name, autofocus: true %>
</div>
<div class="field">
<%= f.label :last_name %>
<%= f.text_field :last_name %></br>
</div>
<div class="field">
<%= f.label :city %>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :address %>
<%= f.text_area :address %>
</div>
<div class="field">
<%= f.label :gender %>
<span class="option">Male</span><%= f.radio_button :gender, "m" %>
<span class="option">Female</span><%= f.radio_button :gender, "f" %></br>
</div>
<div class="field">
<%= f.label :mobile_no %></br>
<%= f.telephone_field :mobile_no %>
</div>
<div class="field">
<%= f.label :website %>
<%= f.url_field :website %>
</div>
<div class="field">
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<% if #validatable %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :skills %>
<%= f.text_field :skills %>
</div>
<div class="field">
<%= f.label :passion %>
<%= f.text_field :passion %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<% end %>
I tried the approach where user enters email and password and after that they are redirected to edit page where they can update other fields.
class RegistrationsController < Devise::RegistrationsController
def after_sign_up_path_for(resource)
redirect_to edit_user_path(resource)
end
end
routes.rb
devise_for :users, :pro, :amateur, controllers: { registrations: "registrations" }
However, the redirect doesn't seem to work. I also tried after_inactive_sign_up_path_for as I am using confirmable, still not working.
I can't seem to figure out why this could be also I would like to know if there are any other approach without using any gem?
You could use javascript if you're just interested in user interface. Or if it's important that you're saving all the info as you're going, you can have the form split into partials and override the devise registration controller to render those separate partials.
You will also need to add steps that link with the partials in your user model. I think if you used a gem, this would probably be main part that it will do for you.
this is a great railscast on how to have a multistep form without any gems. the only difference is that you'll need to override the create method in devise
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.