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
Related
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?
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.
Here is the code for a form created with the bootstrap-form gem for rails.
<%= bootstrap_form_tag(user_sessions_path) do |f| %>
<div class="field">
<%= f.label_tag :email %><br />
<%= f.text_field_tag :email %>
</div>
<div class="field">
<%= f.label_tag :password %><br />
<%= f.password_field_tag :password %>
</div>
<div class="actions">
<%= f.submit_tag "Login" %>
</div>
<% end %>
This throws an error: no implicit conversion of symbol to string on the first line of the form containing the user sessions path. Not sure why this is happening
I believe you have to be explicit about the url. Like this:
<%= bootstrap_form_tag(url: user_sessions_path) do |f| %>
<div class="field">
<%= f.label_tag :email %><br />
<%= f.text_field_tag :email %>
</div>
<div class="field">
<%= f.label_tag :password %><br />
<%= f.password_field_tag :password %>
</div>
<div class="actions">
<%= f.submit_tag "Login" %>
</div>
<% end %>
Check the source code. You can see as how the bootstrap_form_tag method, expects a Hash parameter:
def bootstrap_form_tag(options = {}, &block)
options[:acts_like_form_tag] = true
bootstrap_form_for("", options, &block)
end
I am rolling my own authentication, and the issue I am running into is the edit form for my users. I here is the form...
#app/views/users/edit.html.erb
<h1>Editing User</h1>
<%= render 'form' %>
<%= link_to 'Show', #user %> |
<%= link_to 'Back', root_path %>
#app/views/users/_form.html.erb
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<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 %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="field">
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']) %><br/>
</div>
<div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>
The issue I am having is that when I display the edit form the :user_type field is not correct. I want this field to reflect what is currently saved for the current user, instead I just get the drop down with the first option in the list displayed.
You want to either use:
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], #user.user_type) %>
or:
<%= f.select :user_type, options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel']), :selected => #user.user_type %>
options_for_select has second parameter to preselect it...
options_for_select(['Bar', 'Brewery', 'Restaurant', 'Hotel'], #user.user_type)
I am learning ruby on rails. I want to make editable users account.
I have an error in localhost:
undefined method `model_name' for NilClass:Class
This is my edit.html.erb file:
<h1>Edit user</h1>
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<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 :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 %>
<div>
<%= gravatar_for #user %>
change
</div>
Can you provide a solution?
Most likely your #user object is nil, so when it tries to call #user.model_name, it fails. You should check your controller logic for loading it, and make sure it finds a valid #user.