@ posts = Post. all
end
def new
@ post = Post. new
end
def show
@ post = Post. find ( params [ :id ]
end
def edit
@ post = Post. find ( params [ :id ]
end
def create
@ post = Post. create ( params [ :post ]
end
def update
@ post = Post. update ( params [ :post ]
end
def destroy
@ post = Post. find ( params [ :id ]
@ post . destroy
end
end
Then if we want the calls to some methods (eg show and index) can not be traced we can simply …
Person.add(new Post({id: 1, name: "bean"})) => Console: added bean
Observe remove event on the class collection: Person.observeRemove(function(instance){ console.log('removed ' + instance.get('name')); }); var post = new Person({id: 1, name: "bean"}); Person.add(person);
Person.remove(person.get('id')) => Console: removed bean
Validations
MooModel provides a full validation framework. The following example shows …
…Update documentation on autosave .
Several performance improvements mostly by tenderlove .
Tons of deprecations.
Delicious Digg This Post Facebook Reddit This Post
No related posts.
…:xml def index @ posts = Filter :: Posts .new(params[ :filter ]).all respond_with @ posts end end
As you can see, this places the logic of filtering in its proper place. It's not part of the model, since it's not part of initializing objects, or behavior of individual post objects. It's a separate service, doing something with something else.
These services are incredibly easy to make. Just think about the objects it's trying to handle. These are probably the arguments …
This has been around for a little while now, but Genius Pool now has Twitter integration when a new job is posted. Check it out and post a job.
Delicious Digg This Post Facebook Reddit This Post
@ post = Post. new ( params [ :post ] )
@ post . user = current user
@ post . expiring date = Time . now + 7 . day
@ post . save
end
end
Let us now see how to define a proper method (the Factory Method) of creating the news in the model so, once again, we simplify the controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Post < ActiveRecord:Base
def publish_new_post ( params, user )
post = Self …
…and that we handle the result accordingly. context "updating a post" do setup do @ post = Factory :post Post.stubs(:find => @ post) end
context "when the update is successful" do setup { @post.stubs(:update_attributes => true) }
should set_the_flash.to(/updated/) should redirect_to(posts_path) end
context "when the update is unsuccessful" do setup { @post.stubs(:update_attributes => false) }
should render_template(:edit) …
to instantiate a new Post and render the add view method: # GET /posts/new def new require_login! @ user = User.find @state.user_id @ post = Post.new render :add end
Then finally we will need a route to edit an existing blog entry via HTML, so let's add an edit action method
to lookup an existing Post by id and render it using the edit view method: # GET /posts/1/edit def edit(post_id) require_login! @ …
…Resource public class ShoppingCartController { ... } public class ProductsController { @ Post @ Path ( "/products" ) public void add ( Product product ) { ... } ... }
In the Ruby language, two annotations that are very used are the methods protected and private , which without arguments restrict the visibility of the methods defined below them:
class User def jump; end ; protected def eat; end ; def flirt; end ; privat …
Want To Post A Job?
If you want Ruby Inside and Rails Inside to promote your own Ruby/Rails career openings, you can Post A Job. $ 249 for 60 days of exposure with a link on all Ruby Inside and Rails Inside pages, exposure through Simply Hired, and (usually) inclusion on a post like this that goes out to all 23,000 subscribers. Most ads get 1-2,000 direct views during their run (and linked from about 280,000 pageviews on our sites in all).