Form to One to One Relationship

19 views
Skip to first unread message

Yazaida Hernández

unread,
Jan 17, 2016, 8:36:43 AM1/17/16
to rubyonra...@googlegroups.com
Hello, Im noob in Rails and I have problems with the relationship into
my model User and Profile. I want to create both models with only one
form but i dont know as. I can see the fields of profile but went y
press create only create user. Thanks for help. This is my form.

<%= nested_form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited
this user from being saved:</h2>

<ul>
<% @user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :user_name %><br>
<%= f.text_field :user_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 %></br>
<%= f.password_field :password_confirmation %>
</div>

<%= f.fields_for :profiles do |f1| %>
<div class="field">
<%= f1.label :name %><br>
<%= f1.text_field :name %>
</div>
<div class="field">
<%= f1.label :last_name %><br>
<%= f1.text_field :last_name %>
</div>
<div class="field">
<%= f1.label :birthday %><br>
<%= f1.datetime_select :birthday %>
</div>
<div class="field">
<%= f1.label :avatar %>
<%= f1.file_field :avatar %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And this is my UserController

def user_params
params.require(:user).permit(:user_name, :email, :password,
:password_confirmation, profile_attributes: [:name, :last_name, :avatar,
:birthday])
end
end


Thnks

--
Posted via http://www.ruby-forum.com/.

Walter Lee Davis

unread,
Jan 17, 2016, 10:58:49 AM1/17/16
to rubyonra...@googlegroups.com
Are there any validations on either model? Could you show the model files here?

Walter

Yazaida Hernández

unread,
Jan 18, 2016, 2:33:21 PM1/18/16
to rubyonra...@googlegroups.com
class User < ActiveRecord::Base
attr_accessor :password,:profile_attributes
before_save :encrypt_new_password
validates :email, uniqueness: {case_sensitive: false}, length: { in:
6..20 }, format: {with: /\A(\S+)@(.+)\.(\S+)\z/, message: "Formato de
correo no válido"}
validates :password, confirmation: true, length: {within: 4..20},
presence: {if: :password_required?}
validates :password_confirmation, presence: true

has_one :profile, dependent: :destroy
#Para poder hacer el formulario
accepts_nested_attributes_for :profile, allow_destroy: true
end


class Profile < ActiveRecord::Base
belongs_to :user
belongs_to :role
#validates :user_id, :role_id, presence: true
has_attached_file :avatar, styles: { medium: '200x200>', thumb:
'48x48>' }
validates_attachment_content_type :avatar, :content_type =>
/\Aimage\/.*\Z/

end

class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]

# GET /users
# GET /users.json
def index
@users = User.all
end

# GET /users/1
# GET /users/1.json
def show
end
def show_profile
@user = current_user
@profile = Profile.find_by_id(current_user.profile.id)
end

# GET /users/new
def new
@user = User.new
@user.build_profile
end

# GET /users/1/edit
def edit

end
def cleanup string
string.titleize
end

# POST /users
# POST /users.json
def create
@user = User.new(user_params)
if @user.valid?
@role = Role.find_by(permission: 2)
#@user.create_profile(role_id: @role.id, name:
cleanup(:profile_attributes[:name]))
@user.create_profile(role_id: @role.id)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'Usuario creado
correctamente' }
format.json { render :show, status: :created, location:
@user }

else
format.html { render :new }
format.json { render json: @user.errors, status:
:unprocessable_entity }
end
end
end
end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully
updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status:
:unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was
successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find_by_id(params[:id])
end

# Never trust parameters from the scary internet, only allow the white
list through.
def user_params
params.require(:user).permit(:user_name, :email, :password,
:password_confirmation, profile_attributes: [:id, :user_id, :role_id,
:name, :last_name, :avatar, :birthday])
end
end




these are my files.....
Reply all
Reply to author
Forward
0 new messages