Rails Article Helper - "A" or "An"

Rails article helper - a or an

None that I know of but it seems simple enough to write a helper for this right?
Off the top of my head

def indefinite_articlerize(params_word)
%w(a e i o u).include?(params_word[0].downcase) ? "an #{params_word}" : "a #{params_word}"
end

hope that helps

edit 1: Also found this thread with a patch that might help you bulletproof this more https://rails.lighthouseapp.com/projects/8994/tickets/2566-add-aan-inflector-indefinitize

A/An articles in Ruby

You could define String#articleize to return the appropriate English article:

class String
def articleize
if self[0] =~ /[aeiou]/i
"an #{self}"
else
"a #{self}"
end
end
end

'status'.articleize # => "a status"
'urgent'.articleize # => "an urgent"

rails helper method to find if article belongs to a certain category

This is the wrong use case for a helper method. Helper methods are used to simplify and DRY your views, and occasionally controllers. For example the form helpers make it easier to create forms and bind models to inputs.

Helpers are modules that are mixed into the view context.

What you want here is just a plain old method on your model as it can act on self:

class Article < ApplicationRecord
# ...
def has_category?(name)
categories.exists?(name: name)
end

def internal?
has_category?("Internal")
end
end

Later you can refactor this code into a module if needed but this is not the same thing as a helper. Rather its parallel inheritance through the mixin pattern.

module Categorized
extend ActiveSupport::Concern
included do
has_many :categorizations, as: :resource
has_many :categories, through: :categorizations
end

def has_category?(name)
categories.exists?(name: name)
end
end

class Article < ApplicationRecord
include Categorized
end

class Video < ApplicationRecord
include Categorized
end

You also want to set the dependent: :destroy option on the categorizations join table. The way you have set it up destroying an article will destroy the normalized row in categories!

class Article < ApplicationRecord
has_many :categorizations, dependent: :destroy
has_many :categories, through: :categorizations

class Category < ApplicationRecord
has_many :categorizations, dependent: :destroy
has_many :articles, through: :categorizations

Get data from Helper or use Model in Rails

I would go with (1) your helper method for now. It's simple and straightforward. As I said in the comments, you could use a decorator around your model (using draper for e.g.) to add what I'd consider quite view-specific logic if you are tempted towards option (2).

One note on your helper methods - use pluck instead of collect so you don't select columns or instantiate a bunch of objects you don't need.

Also, order defaults to asc, so you can shorten the whole thing to:

def users_for_select
User.order(:email).pluck(:email, :id)
end

How does articles_path work? (RubyOnRails tutorial)


How action view methods work :

link_to default request type is 'GET'.

button_to default request type is 'POST'.

Each generated route has a specific type, which is how rails map different requests to the correct ones.

For form_for action view helper method, it differentiates between 'POST' and 'PUT' automatically depending on whether you passed an instance or not to the form.

you can also explicitly provide the method type for the form by adding

method: 'GET' OR :html => { :method => 'GET' }

** check different syntax capabilities depending on rails version.

Same goes to other methods, so if you want link_to to send post request you have to pass method="POST" to it.


**How rails differentiate between index and show actions **

In the generated routes table you may have noticed that index action does NOT need an instance id because it is supposed to list all articles. However, for show, you need to pass an instance to it, because it is supposed to show a specific instance only.

= link_to "index", articles_path
= link_to "show", article_path(article)

NOTICE ::

The two methods is not the same, "articles" and "article", plural vs singular. Even if they were identical in names one of them will take an instance and the other won't.

Rails Routing: can't get helper route function to work


rails routes

returns :

  • The route name (if any)
  • The HTTP verb used (if the route doesn't respond to all verbs)
  • The URL pattern to match
  • The routing parameters for the route

see:
http://guides.rubyonrails.org/routing.html#listing-existing-routes

Basically, you should append _path to the route name in your views to call the routes helper.

In your case, the route name is articles so the helper method is articles_path.



Related Topics



Leave a reply



Submit