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.
Related
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
Rails 3.1 RC4
I have a 1:1 association between User and Profile.
When I submit the new profile form, the data I've entered is displayed just fine (see screenshot: http://i.imgur.com/fY8YU.png), but when I refresh it the data is instantly wiped.
Could anyone tell me what is causing this?
Here's the submit form:
<%= form_for([#user, #user.build_profile]) do |f| %>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :picture %><br />
<%= f.text_field :picture %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Here's the users_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb
Here's the profiles_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb
I'm not sure I agree with your approach. Why don't you do something like this:
In models/user.rb:
accepts_nested_attributes_for :profile
In controllers/users_controller.rb:
def new
#user = User.new
#user.build_profile
end
In views/users/_form.html.erb:
<%= form_for #user do |f| %>
<%= f.text_field :first_name %>
<%= f.fields_for :profile do |pf| %>
<%= pf.text_field :some_profile_field %>
<% end -%>
<%- end -%>
This isn't copied or tested, but it should work. On saving your user, the profile fields are sent along and validated with the user fields and are re-rendered when rendering the form again after a save error. This way you will keep full control over your form and its contents with minimal effort.
I am using Acl9 to manage the roles and I want to hide the checkbox usertype if the user has the role :customer and show it if the role is :manager. I want that just the :manager can edit all the fields and some for the :customer.
Thank you for your help!
<h1>Editing user</h1>
<% form_for(#user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :usertype %><br />
<%= f.check_box :usertype %>
</p>
<p>
<%= f.label :surname %><br />
<%= f.text_field :surname %>
</p>
<p>
<%= f.label :firstname %><br />
<%= f.text_field :firstname %>
</p>
<p>
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :registrationdate %><br />
<%= f.datetime_select :registrationdate %>
</p>
<p>
<%= f.label :login %><br />
<%= f.text_field :login %>
</p>
<p>
<%= f.label :password %><br />
<%= f.text_field :password %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', #user %>
<%= link_to 'Back', users_path %>
You can also do think like that:
..in your application_helper.rb
module ApplicationHelper
include Acl9Helpers
end
..and in your views, something like that
<% show_to(:admin) do %>
The content to show
<% end %>
According to the Acl9 documentation, you should be able to do something like this:
<% if #user.has_role?(:manager, nil) %>
<p>
<%= f.label :usertype %><br />
<%= f.check_box :usertype %>
</p>
<% end %>
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 } %>