appropriate usage of hidden_fields - ruby-on-rails

I am sending one parameter ("designer" or "developer") to register form prepared by Devise and I want to add this parameter to devise model.
My question is if assigning parameter to hidden_field is appropriate solution.
From this view I'm redirecting to user registration form
<%= link_to "Register as Owner", new_user_registration_path(:role => 'owner') %>
<%= link_to "Register as Employee", new_user_registration_path(:role => 'employee' ) %>
User registration form
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :role, :value => params[:type]%>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</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="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>

As long as the param doesn't contain sensitive information (passwords etc) as you say it doesn't, there isn't a problem with your implementation.

Related

Devise's edit user page doesn't want to save a text area I've added

As the title says, I've added a text area to devise's edit page, but whenever I click Update, it doesn't update it, it just stays blank all the time.
Here's my edit view:
<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 :current_goals %><br />
<%= f.text_area :current_goals, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, 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" %>
<% if #minimum_password_length %>
<br />
<em><%= #minimum_password_length %> characters minimum</em>
<% end %>
</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 %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
application_controller:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:other_attributes, :current_goals) }
end
However, if I don't define a method called current_goals in my user.rb, rails will give me an error, undefined method current_goals.
user.rb
def current_goals
end
How do I make it save the information I give it?

Updating user information outside devise pages

I want to add the option for the user to edit his email/password/image... from the /settings page not from the /users/edit page. So I copied the code to the settings page
<%= 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>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: 'form-control'%>
</div>
<div class="form-group">
<%= f.label :cover %>
<%= f.file_field :cover, class: 'form-control'%>
</div>
<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 %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
but it returned this
undefined local variable or method `resource' for #<#<Class:0x007f60245ab9b0>:0x007f6026f58358>
What should I add to my controller?
When ever you want to perform any custom actions, first create an action in a controller, then a route to it and then the view file.
resource object is only available in devise routes.
So, first add a method named settings in any controller eg: say userscontroller.
class UsersController < ApplicationController
def settings
#user = current_user
end
end
Then in routes.rb, add a route which points to this action with a route named '/settings'
get '/settings' => 'users#settings', as: :settings
Now, in app/views/users/settings.html.erb
<%= form_for(#user, url: registration_path(#user), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: 'form-control'%>
</div>
<div class="form-group">
<%= f.label :cover %>
<%= f.file_field :cover, class: 'form-control'%>
</div>
<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 %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
So, in the show page of user add a link to settings page,
<%= link_to 'Edit', settings_path(user) %>

Custom field are not showing devise rails

<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 :name %></br>
<%=f.text_field :name , autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%=f.label :User_type %></br>
<%=f.select_tag (:user_type , option_for_select([["Manager",1],["developer",2],["creator",3]])), autofocus: true %>
</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="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "users/shared/links" %>
above is my sign up devise form problem is that i add two new field in this name and user type but these two fields are not showing in the form on screen.....

Using content_for inside a partial with Devise

I am in the process of adding Devise to my application. Everything seems to be working fine but I am struggling to put the sign in and sign up pages into a jquery container on my homepage. I tried pasting the forms in their directly but I was getting "undefined method" errors with the call to resource that devise makes with its forms.
<div class="widget">
<div id="tab-container">
<ul>
<li>Create Account</li>
<li>Sign In </li></ul>
<div id="create">
<%= yield :signup %>
</div>
<div id="signin" style="height: 105px;">
<ul>
<%= yield :signin %></ul>
</div>
</div>
</div>
<% content_for :document_ready do %>
$('#tab-container').easytabs();
<% end %>
</script>
My Sign Up content
<h2>Sign up</h2>
<% content_for :signup do %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name))
do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :username %><br />
<%= f.text_field :username %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<% end %>
and my Sign in content
<h2>Sign in</h2>
<% content_for :signin do %>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do
|f| %>
<div id="login_form">
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<% if devise_mapping.rememberable? -%>
<div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
<% end -%>
<div><%= f.submit "Sign in" %></div>
</div>
<% end %>
<% end %>
Is it not a good idea to use content_for inside a partial? Is the yield not rendering before the tab container is called? Thanks in advance!
Found the answer here Devise form within a different controller.
Add this to your application helper
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end

DRY form partial for create and update

I have a _form.html.erb form partial which helps to DRY up my code but I need the form to have different labels depending on if I am creating a new user or updating an existing user.
Here is my form partial. I don't need to show the eula checkbox during update and I also need to replace the "Create my account" submit button text to something more appropriate when doing an update.
<% form_for #user do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, 'Full name' %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email, 'Email address' %><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.check_box :eula %>
<%= f.label :eula, 'I agree to the terms and conditions' %>
</p>
<p><%= f.submit "Create my account" %></p>
<% end %>
Which one of the following is the best way to do this?
have 2 separate form partials, one for create and one for update
have 1 form partial but have conditional labels based on the action (is this possible?)
factor the common part into a partial and reuse that in the create and update forms
If I were to do conditional form how would I check which action is being performed?
ActiveRecord has the new_record? method which you can use to decide what to show on the form:
<% form_for #user do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, 'Full name' %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email, 'Email address' %><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>
<% if #user.new_record? %>
<p>
<%= f.check_box :eula %>
<%= f.label :eula, 'I agree to the terms and conditions' %>
</p>
<% end %>
<p><%= f.submit #user.new_record? ? "Create my account" : "Update my account" %></p>
<% end %>
Wrap the <form> tag around the call partial tag and put the submit button in the respective views. Only put the eula check box in the create view.
You can create a variable in the new and update views and use that as your label name.
<%= f.label email, emaillabel %>
[Edit]
If you need to pass variables to a partial use this:
<%= render :partial => 'form', :locals => { :myvar => myvar } %>

Resources