mdbhal
mdbhal

Reputation: 99

Routing in Rails

I am new to Ruby on Rails.I am following Rails Guides tutorial. I've built a basic form which takes title and text as an input and stores in the database. Here is my code:

ArticlesController:

class ArticlesController < ApplicationController

  def new
  end

  def create
    @article = Article.new(article_params)
    @article.save
    redirect_to @article
  end

  def show
    @article = Article.find(params[:id])
  end

  private

  def article_params
    params.require(:article).permit(:title, :text)
  end
end

routes.rb:

Rails.application.routes.draw do
   get 'welcome/index'
   root 'welcome#index'
   resources :articles
end

new.html.erb(the form):

<h1>New article</h1>
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
<p>
 <%= form.label :title %><br>
 <%= form.text_field :title %>
</p>
<p>
  <%= form.label :text %><br>
  <%= form.text_area :text %>
</p>
<p>
  <%= form.submit %>
</p>

After I click submit button of the form, I am taken to the show view where I display the data of the form. But I am not able to understand how exactly this is happening? Nowhere have I mentioned to render the show.html view, so how does Rails understand which view I have to render? I am a bit confused with this.

Note: I come from a Django background, so how I would implement routing in Django is by defining the required method(from views.py) in my urls.py for the corresponding route. Then I would render the template defined in the method in views.py.

Upvotes: 0

Views: 131

Answers (4)

current_user
current_user

Reputation: 1212

From your code

 def create
          @article = Article.new(article_params)
          @article.save
         redirect_to @article
      end

after @article is successfully saved, you are redirecting it to show action by specifying redirect_to @article

If you do rake routes

    article GET    /article/:id(.:format)      articles#show

so the actual route for show action is article_path(@article).You can redirect to the show action by writing redirect_to article_path(@article) or even if you simply pass an object (@article) to redirect_to method, rails will redirect it to the corresponding show action because of syntactic sugar of redirect_to url_for(@page)

Upvotes: 0

nitanshu verma
nitanshu verma

Reputation: 261

Please complete the routes guide

But for now as

article GET    /articles/:id(.:format)

and in your create action in the last line you wrote

redirect_to @article

which leads to you to the /articles/:id of the instance variable @article

Upvotes: 0

krishna raikar
krishna raikar

Reputation: 2675

rails g scaffold article title:string text:string

run above command and you will get complete application what you are looking for. You will understand what is needed in routes.

Upvotes: 0

sa77
sa77

Reputation: 3603

You have defined resources :articles on your config/routes.rb file which is a rails helper method that generates the following routes when you run rake routes | grep articles on your terminal shell

            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

The statement redirect_to @article at the end of def create ... end block is translated by rails to 'redirect_to /articles/@article.id' which is responsible for redirecting the response to articles#show action defined in app/controllers/articles_controller.rb

Upvotes: 2

Related Questions