Skip to content

Instantly share code, notes, and snippets.

WORK:
<%= form_for @user do |f| %>
<div class="field">
<%= f.label :street %><br />
<%= f.text_field @user.address.street %>
</div>
<% end %>
DONT Work
it "should inform the user on failed save" do
user = Factory.create(:user)
user.stub(:valid?).and_return(false)
post :create, :user => user
flash[:notice].should_not be_nil
end
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to(@user) }
else
format.html { redirect_to(@user, :notice => 'Error') }
user = Factory.create(:user)
user.stub(:valid?).and_return(false)
post :create, :user => user
# Controller
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to(@user, :notice => 'instructions for confirming') }
else
format.html { redirect_to(@user, :notice => 'Error') }
end
user = Factory.build(:user)
user.should_receive(:save).and_return(true)
User.stub(:new).and_return(user)
class User < ActiveRecord::Base
belongs_to :address
accepts_nested_attributes_for :address
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :confirmable, :lockable, :validatable
# Setup accessible (or protected) attributes for your model
context "correct format" do
it "should be invalid with an invalid email address" do
invalid_email_addresses.each do |invalid|
@user.email = invalid
@user.should_not be_valid
end
end
it "should be valid with an valid email address" do
class CreateOffers < ActiveRecord::Migration
def self.up
create_table :offers do |t|
t.string :title
t.string :description
t.integer :seller_id ## The user_id from the seller
t.integer :buyer_id ## The user_id from the buyer
t.timestamps
end
end
class Offer < ActiveRecord::Base
belongs_to :user
attr_readonly :user
attr_accessible :user,:title,:description,:price
validates :user, :presence => true
validates :title, :presence => true
validates :description, :presence => true
validates :price, :presence => true