Two form values are not being saved - ruby-on-rails

I have this signup for with devise:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<p><%= f.label :email %><br />
<%= f.email_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>
<%= f.fields_for :profile, resource.build_profile do |t| %>
<div class ="field">
<%= t.label :type, "Are you an artist or listener?" %><br />
<div>Artist: <%= t.radio_button :type, "Profile::Artist", :id => "artist_button" %></div>
<div>Listener: <%= t.radio_button :type, "Profile::Listener", :id => "listener_button" %></div>
</div>
<% end %>
<p class="before"><%= f.submit "Sign up", :id => "new_user_submit" %></p>
<% end %>
Then I have some JQuery that inserts a field dynamically:
$("#artist_button").click(function() {
if ($('input#user_name').parent().hasClass('field')){
$('input#user_name').parent().remove();
}
$(".before").before('<div class="field"><label for="user_name">Artist or Band/Group Name</label><br><input id="user_name" name="user[name]" size="30" type="text"></div>');
});
However, the type attribute that is nested in the user form and the name attribute that is inserted dynamically are not being saved to the database. The values are being entered as null.
Why is this? How can I fix this?
UPDATE:
The name attribute was not attr_accessible and that was causing it to not be saved to the db. However, the type attribute, which is nested inside the user form, is attr_accessible but is still not being saved to the db.

"type" is a "magic" ActiveRecord column name used for single table inheritance which means that you just got hit by convention-over-configuration. You need to rename your "type" column to something else - I usually use "kind" as a column name.

Try putting in your User Model:
attr_accessible :profiles_attributes
along with the rest of devises attr_accessible(email,remeber_me,etc) , do not delete what you have!

Is type an association? If so, you'd have to add :type_id to attr_accessible, too.

Did you add
accepts_nested_attributes_for :profile
in your User model?

Related

Registration Form: Unable to Hide Field

In creating a new application I have used marklocklear example (https://github.com/marklocklear/devise_multi_tentant) for assistance in creating a multi-tenant environment. It works well.
The next thing I wanted to do I thought would be simple but has turned into quite the niggle for me.
All I wanted to do is rather than having the initial admin user have to enter an organization name (which is not necessary in my application), I wanted to hide that field.
Sign Up Form (that works):
<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= 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>
<%= f.fields_for :organization do |org| %>
<div><%= 'Organization or Company Name' %><br />
<%= org.text_field :name %></div>
<% end %>
<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 %>
<%= render :partial => "devise/shared/links" %>
So, I did this to try to hide the field:
<%= f.fields_for :organization do |org| %>
<%= f.hidden_field :name %>
<% end %>
But I get an error.
The baffling thing to me is that if I leave the field in, but do not enter a value everything still works properly -- this is unexpected. Since (as part of my troubleshooting) I removed the field completely -- and got an error message stating the form could not be processed because the organization part was blank.
Any assistance would be appreciated.
Max: Thank you. As you know, that worked like a charm. The field is now hidden and the form submits properly. Thanks.
<%= f.fields_for :organization do |org| %>
<%= org.hidden_field :name %>
<% end %>

rails form_for mongoid error

Hi i have a simple form for updating a user's account here is my controller
def edit
#users = User.find(params[:id])
end
here is my view
<%= form_for :user do |f| %>
<%= f.label :first_name %> <br />
<%= f.text_field :name ,:value => #users.name%><br />
<%= f.label :last_name %> <br />
<%= f.text_field :lname ,:value => #users.lname%><br />
<%= f.label :email %> <br />
<%= f.email_field :email ,:value => #users.email%><br />
<%= f.label :id %> <br />
<%= f.text_field :id ,:value => #users.id%><br />
<br />
<%= f.submit ({:confirm => "Are you sure?"}) %>
<% end %>
when i click submit i get the following mongoid error
Mongoid::Errors::InvalidFind in UsersController#edit
Problem:
Calling Document.find with nil is invalid.
Summary:
Document.find expects the parameters to be 1 or more ids, and will return a single document if 1 id is provided, otherwise an array of documents if multiple ids are provided.
Resolution:
Most likely this is caused by passing parameters directly through to the find, and the parameter either is not present or the key from which it is accessed is incorrect.
I'm fairly novice with rails and this is my first stab with mongoDB. Any input would help, Thank you.
Try to use this form
<%= form_for(#users) do |f| %>
...
...
<% end %>
The error comes because you are using :user that invalid find in your user controller

update a table which in not devise model on sign up using devise?

How to add value from devise registration form to another table that is not devise table
this is sample code
<div class="signin">
<h2>Register</h2>
<%= 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 :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :full_name %><br />
<%= f.text_field :full_name %></div>
<div><%= f.submit "Register" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
</div>
full name is field of profile table and i set device on user table,
and if user enter name i want name full name store in profile table
i dont have any idea how can i do this?
you can use one of the many hooks that are provided by active record models:
http://guides.rubyonrails.org/active_record_validations_callbacks.html#creating-an-object
ie. you could retrieve the profile object of the user, update the record and save it back in an before_save hook.
I got the answer
Put a def in devise model def name same as field name then devise allow you to pass the that field to model, in model/controller you can insert value through params in any table.

How to add CarrierWave uploader to Devise registration view? (rails gems)

When users register, I want them to be able to upload an avatar. The new registration .html.erb looks like this:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:multipart => true} ) do |f| %>
<%= devise_error_messages! %>
<div>
<%= f.label :username %><br />
<%= f.text_field :name %>
</div>
<div>
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
<div><%= f.label :email %><br />
<%= f.email_field :email %></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 %>
<%= render "links" %>
User.rb:
class User < ActiveRecord::Base
....
mount_uploader :image, ImageUploader
ImageUploader exists...
When I try to visit the view, it tells me that User::ImageUploader doesn't exist. When I try to specify the fully qualified name of the class in the user model, either as an 'include' statement or as an argument to the mount_uploader function, it can't find that either. Do I need to somehow build in a separate form_tag in that view? I'm sure this has been done before. Any help is appreciated.
Have you:
Added an :image column to your User model?
Defined the uploader app/uploaders/image_uploader.rb?
Restarted your server?
You shouldn't need to have this as a separate form tag, as long as the image attribute exists on the User model.
You may need to check your form markup. I notice you have :username on the label and :name on the field.
Try changing:
<%= f.label :username %><br />
<%= f.text_field :name %>
to
<%= f.label :name, "username" %><br />
<%= f.text_field :name %>

nested attributes for registration form

I have a user and a company model where a user has_one company and company belongs to user. I just wanted to save the company field from the registration form to the company table,where the company table columns are user_id and company_name. I used the following to do this in my User.rb
has_one :company, :class_name => 'Company', :foreign_key => :user_id, :dependent => :destroy
accepts_nested_attributes_for :company
And My registration form looks like this
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :firstname %><br/>
<%= f.text_field :firstname %></p>
<p><%= f.label :lastname %><br/>
<%= f.text_field :lastname %></p>
<p><%= f.label :email %><br/>
<%= f.email_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>
<%= fields_for :company do |builder| %>
<%= builder.label :company %>
<td><%= builder.text_field :company_name%> </td>
<% end %>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
Is it necessary to have a hidden field for user_id for company table or it will get automatically added, I also tried overriding the create method of registration controller it works but the devise error messages are not working. Can anyone help me out in this. I dont know what I have missed. Thanks in advance.
1) No, you don't need a user_id hidden field, just use fields_for as a method
<%= f.fields_for :company do |builder| %>
...
<% end %>
2) fields_for will not be shown if user does not have associated companies yet, and that will happen with new record, so you can do
<% f.object.company.build %> as a hack :)
or do it the proper way in a controller action

Resources