Skip to content

Instantly share code, notes, and snippets.

@mankind
Last active October 24, 2022 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mankind/0d7f7f51c1397028c14ee5236a8134ec to your computer and use it in GitHub Desktop.
Save mankind/0d7f7f51c1397028c14ee5236a8134ec to your computer and use it in GitHub Desktop.
mapping python & django to ruby & rails
django vs rails

django views map to rais controllers

polls/views.py¶

from django.http import HttpResponse

from .models import Question

django templates are rails views

django index template
{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}
rails index template
<% if @latest_questions_list.each do |question| %>
  <li> <%= link_to question.name, question %> </li>
<% else %>
  <p> No polls are available </p>
<% end %>
routing in django and rails

This is django:

Using angle brackets “captures” part of the URL and sends it as a keyword argument eg question_id=34 
urlpatterns = [
    - ex: /polls/
    path('', views.index, name='index'),
    - ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    - ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    - ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

is similar to this in rails

Rails.application.routes.draw do
   - dynamic segment
   - path of /photos/1/2 will be dispatched to the show action of the PhotosController
   - params[:id] will be "1", and params[:user_id] will be "2".
   get 'photos/:id/:user_id', to: 'photos#show'

   - incoming path of /photos/1?user_id=2 
   - params will be { controller: 'photos', action: 'show', id: '1', user_id: '2' }
   get 'photos/:id', to: 'photos#show'

  resources :polls do
    - if only one member route use:
    get 'results', on: :member
      
    - multiple meber routes example
    members do
      - polls/5/results
      get :results
      
      - /polls/5/vote
      get :vote
    end
    
    - /polls/search
    collection do
      get 'search'
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment