I have a from like this:
<% form_for(#user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</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.label :role %> <br/>
<%= f.text_field :role%>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
In the database, role is a "Char" field. I want it different from a textfield, the user can select "Teacher", "Student", if the user select "Teacher", the database will store "T", otherwise, it will store "S". How can I do so? It is necessary for me to add a "User role" table in the database, and then make a relationship with the user? But it is necessary to do it this way? thank u.
Ref select and options_for_select
<%= f.select :role, options_for_select([["Teacher", "t"], ["Student", "s"]]) %>
Related
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
Hi sir in rails 4 while using form_for also getting above error in google i read about this and follow that but even also i am getting same error
if i disable in application controller and added protect_from_forgery with: :null_session it is working but when i submit the form_for form i am getting params missing or params should not empty .
i refered this link i refered linkeven also not working
Error in the log file
Processing by UsersController#create as HTML
Can't verify CSRF token authenticity
code view
<%= form_for #user do |f| %>
<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 :gender %><br />
<%= f.text_field :gender %>
</p>
= f.fields_for :address do |p| %>
<p>
<%= p.label :city %><br />
<%= p.text_field :city %>
</p>
<p>
<%= p.label :state %><br />
<%= p.text_field :state %>
</p>
<p>
<%= p.label :country %><br />
<%= p.text_field :country %>
</p>
<p>
<%= p.label :postal_code %><br />
<%= p.text_field :postal_code %>
</p>
<% end %>
<p><%= f.submit %></p>
<% end %>
if set protect_from_forgery with: :null_session and if submit the form
than i will getting one more error
param is missing or the value is empty:
userActionController::ParameterMissing (param is missing or the value is empty: user):
I have a user model with a settings page at /users/1/edit
Currently you can change the email and change your password (w/ confirmation).
I would like to have a field called "current_password" where the user has to type their current password before they can change any information.
Here is my form now:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
Obviously if I just add the field in, it gives me an error because my user model doesn't have a "current_password" attribute.
How can I do this? Is there a better way, possibly?
You can either add it to the model and database or this to your model:
add attr_reader :current_password
Then you can do a validation of that in your model.
I am having trouble using the Ruby Gem paperclip. I followed the instructions in the ReadMe but I cannot seem to get it to actually load my images. Here is my edit form:
<% form_for :user, #user, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :expertise %><br />
<%= f.text_area :expertise, :class => "expertise" %>
</div>
<div class="field">
<%= f.label :occupation %><br />
<%= f.text_field :occupation %>
</div>
<div class="field">
<%= f.label :city %><br />
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<%end%>
Yet when I try to save I keep getting this error: No route matches "/users/4/edit"
What is the problem
The error is telling you that there's no /users/4/edit route. What does your config/routes.rb look like? If there's a line like:
resources :users
Then, try changing that first line to:
form_for #user
Instead of:
form_for :user, #user
Also, I don't see a file_field in there anywhere so I don't think this question is about Paperclip?
This is basically a nested form question, albeit with only one field that belongs to a parent model. My data entry form collects data for a model - however I also need to collect one other a data element/value (UserID) that actually goes into a parent record that will be created with the detail record.
AFAIK Rails expects each form field to map to a model and I need to create an unbound data input field that I will use separately.
How can I override this default behaviour and create a'free form/unbound field'?
TIA,
BC
Heres something from my own app:
Access it by:
params[:company] and params[:user]
Controller:
#company = Company.new
#user = User.new
View:
<% form_for #company, :url => companies_path do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :website %><br />
<%= f.text_field :website %>
</p>
<hr />
<% fields_for #user do |u| %>
<p>
<%= u.label :email %><br />
<%= u.text_field :email %>
</p>
<p>
<%= u.label :username %><br />
<%= u.text_field :username %>
</p>
<p>
<%= u.label :password %><br />
<%= u.password_field :password %>
</p>
<p>
<%= u.label :password_confirmation %><br />
<%= u.password_field :password_confirmation %>
</p>
<% end %>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
For the "magic" form <=> model mapping form_for is used. (http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html)
If you need something out of the current model try using http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html
With that you can add tags separate from the model, eg
radio_button_tag
inside the form_for block