Getting info from one form, saving to another model - ruby

I have 2 models
User
Profile (belongs_to User)
I have 'username' in Profile model. I want to take value of 'username' in
User new form and save it to username of Profile. How can I do that. Please suggest code.

Try this
Add these in your Gemfile
gem 'simple_form'
gem 'nested_form'
In your user model add this
accepts_nested_attributes_for :profile
In your form under simple_nested_form_for
= f.simple_fields_for :profile do |p|
= p.input :username
In you user controller
add this to permit profile parameters
def user_params
params.require(:user).permit(
#User Attribute Names,
profile_attributes: [#Profile atrribute names]
)

Related

Rails 4 Active Admin, Attribute Won't Save

I'm currently using rails 4 and active admin for my admin interface. I have a profile which has many analytics_by_weeks.
class Profile < ActiveRecord::Base
has_many :analytics_by_weeks
I want to use active admin to create new analytics_by_weeks and link them to a profile (following my belongs_to has_many relationship), but when I click create to generate the new analytics_by_week the profile attribute will not save.
In my program I added a name attribute to a profile:
add_column :profile, :name, :string
I have tried making a custom formtastic form:
f.input :profile, :collection => Profile.all.map{ |p| [p.name, p.id] }
Nothing has worked, what am I doing wrong? I have also used permit parameters to permit profile, id, and name.

Using attr_encrypted with DataMapper and Sinatra

I'm quite new with DataMapper and Sinatra and especially attr_encrypted. What I want is to store my passwords encrypted and then be able to search for a user by username and password. I read the documentation of attr_encrypted, but I still don't get what to do :(
Can you please give my some example of a project using these two technologies or tell how to change my code to work :(
My User Class:
class User
include DataMapper::Resource
attr_encryptor :password, :key => 'secret key'
property :id, Serial
property :encrypted_password, Text
end
When I save a User, I do it this way:
username = params[:username]
password = params[:password]
user = User.new(:username => username, :encrypted_password => password)
user.save
which is saving the original password, not the encrypted one.
And I have no idea how to search for users when the password is encrypted :(
Now it is something like this:
#user = User.all(:username => username, :password => password)
Please excuse me for the newbie quiestion, but I really can't quite understand it :(
Thank you very much in advance!
You need to add the attr_encryptor line after you have specified the Data Mapper properties. This prevents DataMapper simply replacing the encrypted_password accessors with its own:
class User
include DataMapper::Resource
property :id, Serial
property :encrypted_password, Text
# this line moved down from above
attr_encryptor :password, :key => 'secret key'
end
and then create the user with:
user = User.new(:username => username, :password => password)
Are you sure you want to search for a User based on an encrypted password? Normally you would find the user based on e.g. username and then just check the password matches.
If you do want to do this, you will have to either recreate the encrypted password in your code and search with that (you’ll need to check the docs to see how encryption is done):
User.all(:username => username, :encrypted_password => encrypt(password))
Alternatively fetch all matching users and filter them in your code:
User.all(:username => name).select {|u| u.password == password}
Your encrypted password is :password, so you have to do
User.new(:username => username, :password => password)
To find an user by username and password, you should just do
User.first(:username => username, :password => password)
Anyway you could avoid that gem using instead bcrypt doing a thing like this.

Accessing more than one active records in action controller in rails 3

suppose I have 2 models: 1. user and 2. userProfile
we have one-to-one relationship between 2 models.
Then,I would like to create Usercontroller and create user with fields such as username and password in user and other details like address, mobile number,etc in userprofile.
user_id is a foreign key in user_profile
Then I created new.html.erb in user a view that render all above fields in a same form.
Then how I can save the data in two different tables user and userprofile respectively.
what I tried is
1. has_one :user_profile in user.rb and belongs_to :user in user_profile.rb
2. accepts_nested_attributes_for :user_profile in user.rb
user_controller:
def create
#user = User.new(params[:user])
if #user.save
#userprofile = #user.create_user_profile(params[:user_profile])
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
3. Then I got error the only userid is created in user_profile and also update run to make user_id nil which can't be! so raising error:
Unknown column 'user_profiles.' in 'where clause': UPDATE `user_profile`
SET `user_id` = NULL WHERE `user_profiles`.`` IS NULL
so what's wrong and how to troubleshoot this?
If you're using accepts_nested_fields_for then you don't need to manage saving the UserProfile records yourself. ActiveRecord will take care of it for you.
To get this to work you need to:
Make sure you have accepts_nested_fields_for :user_profile in your User model
Update your app/views/users/_form.html.erb to include form elements for the UserProfile, e.g.:
<%= f.fields_for :user_profile, #user.user_profile do |profile_form| %>
<%= profile_form.text_field :address %>
<%= profile_form.text_field :mobile_number %>
<% end %>
Update your UsersController#new action to build the UserProfile for the User, e.g.
def new
#user = User.new
#user.build_user_profile
(...)
end
For more information see the Rails API docs covering Active Record Nested Attributes.

Paperclip dosnt save attachments if password has attr_accessor

My project:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :email,
:password,
:password_confirmation,
:first_name,
:last_name,
:birth_date,
:residence,
:user_role,
:show_email,
:avatar
as_attached_file :avatar,
:default_url => '/images/system/user_avatars/default/default_avatar.png',
:url => "/public/images/system/user_avatars/:id_:style.:extension",
:path => "/public/system/user_avatars/:id_:style.:extension"
def update_profile(user_id, params) #params has :category and :user params
#user = User.find(user_id)
#user.update_attributes(params[:user])
return params[:category]
end
end
So, from my controller i call this method and i get no error. Paperclip shows attachment saved. The database is updated, but the image file is not saved. I have an registration made from scratch, so that's why i have the "attr_accessor :password"
I checked:
Have :multipart => true in form
Have attr_accessible :avatar in user model
Can any one give me some lead, cos i cant figure, why paperclip dosnt save the file.
Set attr_accessible :avatar_file_name as well, and you also need a paperclip.rb initializer:
require "paperclip"
Paperclip.options[:command_path] = "/ImageMagick"
And, of course, have ImageMagick installed.

How to retrieve an object from a CouchDB view using the CouchRest Ruby Gem

Assuming I have a view in my CouchDB named "user/all" and a CouchRest ExtendedDocument as follows:
class User < CouchRest::ExtendedDocument
property :username
property :password
property :realname
property :role
property :rights
end
How would I go about retrieving a document for the key 'admin' from this view using this ExtendedDocument?
(If I need to make changes to the ExtendedDocument subclass, what should be changed?)
Many thanks.
Try this:
class User < CouchRest::ExtendedDocument
property :username
property :password
property :realname
property :role
property :rights
view_by :role
end
Here, I am assuming 'admin' is a role property. This will make a view in your design document keyed by role. Then, to get all 'admin' documents, you just do the following:
#admins = User.by_role(:key => 'admin')
If in fact the actual id of the document is 'admin', then all you have to do is this:
#admin = User.get('admin')
Or, alternatively:
#admin = User.all(:key => 'admin')
I would also suggest taking a look at CouchRest Model, which is basically an Active Model complaint extension to CouchRest if you are using this with Rails. Good Luck!

Resources