Reputation: 135
I am new to ruby on rails and i want to understand how this piece of code redirect to the show action thanks :
def create
@article = Article.new(params[:article])
@article.save
redirect_to @article
end
Upvotes: 4
Views: 154
Reputation: 76784
I am new to ruby on rails
To understand how it works, you need to understand the nature of Ruby, rather than Rails.
Because Rails is built on top of Ruby, it's an exponent of the object orientated programming pattern - the idea that every element of your application should be geared towards the creation & manipulation of objects:
Although the answer to your question lies in the Rails application of this methodology, its understanding begins in how Ruby gets it to work.
In order to have a truly object orientated experience, Ruby builds classes which are available in memory to manipulate & utilize. These classes are invoked, and have a series of methods which you can use to interact with them.
In the sense of Rails (I'm not super experienced with Ruby), these methods come in two forms:
- Class methods (
def self.x
)- Instance methods (
def x
)
These work to give you the ability to call these methods to manipulate your object as you see fit. For example, if you were creating a game with aliens, you could have @alien.is_visible?
This is the underline structure which Ruby gives us.
Rails
Because Rails is built on top of Ruby (Ruby being the language, Rails being the framework), your answer lies in how Rails manipulates the objects you create.
To understand it properly, you need to know that Rails is an MVC (Model View Controller) framework:
As you can see, Rails builds your objects in your Models. This is why you can call @article = Article.find x
.
Thus, when you ask how redirect_to @article
works, you need to remember that Rails builds its objects in a certain way (IE with a series of base methods - inherited from ActiveRecord::Base
).
These methods give each model object a certain structure, which Rails can use within its helpers. This is the case with redirect_to
:
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
Rails expects its objects to conform to a certain structure, backed up by CRUD / RESTful routes:
Thus, if you have @article
(which is to conform with Rails' model structure, and you have the routes set up as below):
#config/routes.rb
resources :articles
Rails will be able to take your instance of the Article
object, and use it to populate the redirect_to
route.
--
This is how Rails populates many of its helper methods, including form_for
and render
. It's intelligent guessing, based on the conventions which govern the framework.
It's why Rails programmers are often eager to point out how "Railsy" their code is.
Upvotes: 1
Reputation: 534
Basically you have the following route:
Prefix Verb URI Pattern Controller#Action
article GET /articles/:id(.:format) articles#show
In order to redirect to the show
action of a specific article, you need to end up with a string of the form '/articles/:id', and there are several levels of syntastic sugar to accomplish this:
redirect_to @article
redirect_to article_path(@article)
redirect_to article_path(@article.id)
You could also explicitly specify the path, or even the full URL:
redirect_to "/articles/#{@article.id}"
redirect_to "http://myapp.com/articles/#{@article.id}"
although I wouldn't recommend that.
See the API docs for more ways of using redirect_to
Upvotes: 2
Reputation: 34338
If you define your routes using the Rails convention of defining RESTful routes i.e. resources :articles
in your routes.rb
file, then redirect_to @article
will take you to the show
page of this particular @article
instance. Rails is doing the underlying magic here.
When you write resources :articles
in your routes.rb
file, Rails is generating these routes for you automatically:
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
So you have this particular route which is mapped to articles
controller's show
action:
article GET /articles/:id(.:format) articles#show
This route is matched when you do: redirect_to @article
and that is why it's taking you to the show
page of this @article
.
To know more about how RESTful routes works, see this Rails tutorial
Upvotes: 3