Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does article_path mean?

I am quite new to programming and am working my way through the official Getting Started guide. This guides new RoR programmers through a practical Blog application involving CRUD articles that have many comments.

article_path is something that comes up quite often but I have no idea at all at what this does, what it means, how it is used etc.

Here are some examples of how the guide uses article_path:

<%= form_for :article, url: articles_path do |f| %>

(in app/views/articles/new.html.erb)

<%= link_to 'Back', articles_path %>

(also in app/views/articles/new.html.erb)

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

 redirect_to articles_path
end

(in app/controllers/articles_controller.rb)

class CommentsController < ApplicationController
 def create
  @article = Article.find(params[:article_id])
  @comment = @article.comments.create(comment_params)
  redirect_to article_path(@article)
 end

(in app/controllers/comments_controller.rb)

I would be very grateful for an explanation, as I like to fully understand what a specific line of code does.

Thank you for your time

like image 681
Brian Hudson Avatar asked Feb 06 '26 11:02

Brian Hudson


2 Answers

articles_path will produce something like this http://yourserver.com/articles which will be associated with your ArticlesController and index action.

Consequently article_path(@article) will produce something like this http://yourserver.com/articles/1234567 which will be associated with your ArticlesController and show action and produce a page for an article with id 1234567.

You also can do rake routes on the command line and see all the details

like image 188
Zepplock Avatar answered Feb 09 '26 01:02

Zepplock


The following in your routes.rb file:

match '/articles' => 'articles#index', :via => [:get], :as => :articles

Can be invoked my mentioning articles_path. In the above example any request given to domain.com/articles will be mapped to articles#index. Instead of making request to '/articles' you can just use 'articles_path'

Another Example:

match '/articles/all' => 'articles#getAll', :via => [:get], :as => :articles_complete

Any request to yourserver.com/articles/all will be mapped to articles controller and getAll Action. Instead of mentioning '/articles/all', you can just invoke 'articles_complete_path'. I'm naming articles_complete instead of articles_all to show the convention used.

like image 39
user1801879 Avatar answered Feb 09 '26 00:02

user1801879



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!