Created
December 29, 2011 19:55
-
-
Save anonymous/1535921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user.rb | |
class User < ActiveRecord::Base | |
has_one :address, :dependent => :destroy | |
validates_presence_of :nome, :nick, :cognome, :message => "è un campo obbligatorio" | |
validates_uniqueness_of :nick, :message => "è un camppo univoco, ripeti inserendo un nuovo nick" | |
accepts_nested_attributes_for :address, :allow_destroy => true | |
end | |
new.html.erb | |
<h1>Inserisci un nuovo utente</h1> | |
<% form_for(@user) do |f| %> | |
<%= f.error_messages %> | |
<p> | |
<%= f.label :nick %><br /> | |
<%= f.text_field :nick %> | |
</p> | |
<p> | |
<%= f.label :nome %><br /> | |
<%= f.text_field :nome %> | |
</p> | |
<p> | |
<%= f.label :cognome %><br /> | |
<%= f.text_field :cognome %> | |
</p> | |
<p> | |
<%= f.label :email %><br /> | |
<%= f.text_field :email %> | |
</p> | |
<fieldset> | |
<p>Indirizzo | |
<% f.fields_for :address do |address_fields| %> | |
<p> Via : <%= address_fields.text_field :indirizzo %> </p> | |
<p> Città: <%= address_fields.text_field :citta %> </p> | |
<% end %> | |
</p> | |
</fieldset> | |
<p> | |
<%= f.submit 'Crea' %> | |
</p> | |
<% end %> | |
<%= link_to 'Indietro', users_path %> | |
address.rb | |
class Address < ActiveRecord::Base | |
belongs_to :user | |
end | |
user_controller.rb | |
class UsersController < ApplicationController | |
# GET /users | |
# GET /users.xml | |
def index | |
@users = User.all | |
respond_to do |format| | |
format.html # index.html.erb | |
format.xml { render :xml => @users } | |
end | |
end | |
# GET /users/1 | |
# GET /users/1.xml | |
def show | |
@user = User.find(params[:id]) | |
@address=@user.address | |
respond_to do |format| | |
format.html # show.html.erb | |
format.xml { render :xml => @user } | |
end | |
end | |
# GET /users/new | |
# GET /users/new.xml | |
def new | |
@user = User.new | |
@address=@user.build_address | |
respond_to do |format| | |
format.html # new.html.erb | |
format.xml { render :xml => @user } | |
end | |
end | |
# GET /users/1/edit | |
def edit | |
@user = User.find(params[:id]) | |
@address=@user.address | |
end | |
# POST /users | |
# POST /users.xml | |
def create | |
@user = User.new(params[:user]) | |
if @user.save | |
redirect_to :action => 'show', :id => @user | |
flash[:notice] = "Your record has been saved." | |
else | |
render :action => 'new' | |
end | |
end | |
# PUT /users/1 | |
# PUT /users/1.xml | |
def update | |
@user = User.find(params[:id]) | |
respond_to do |format| | |
if @user.update_attributes(params[:user]) | |
format.html { redirect_to(@user, :notice => 'Modifica effettuata con successo.') } | |
format.xml { head :ok } | |
else | |
format.html { render :action => "edit" } | |
format.xml { render :xml => @user.errors, :status => :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /users/1 | |
# DELETE /users/1.xml | |
def destroy | |
@user = User.find(params[:id]) | |
@user.destroy | |
respond_to do |format| | |
format.html { redirect_to(users_url) } | |
format.xml { head :ok } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment