Today I learn a lot about how Rails work from my awesome colleague, Hass. He thought me a lot of stuffs, which I will try to break down as detailed and little as possible.
=, == and ===operators
u = User.new # assign variables p = u.update(phone: 0123456789) u.phone == "0123456789" # this will return true u.phone === "0123456789" # strict comparison # this will return false; because it was integer compared against string
namespace :agent do resources :profiles, only: [:index, :show] do put :verify, on: :member put :unverify, on: :member put :enable, on: :member put :disable, on: :member end end
Basically, how I read and understand from the code above is, for :profiles resource (and only on index and show methods inside agent profile controllers), allows these PUT requests on all of these methods (:verify, :unverify, :enable, and :disable), specifically, on member.
Based on the image below, so
on: :member means the request only appliable to single
:id for instance:
PUT /agent/profiles/:id/unverify(.:format) agent/profiles#unverify PUT /agent/profiles/:id/enable(.:format) agent/profiles#enable
:collection it means that it applies to the whole namespace or resources.
So basically, in views, I am using an anchor link helpers on button with
remote: true. Previously for the specific feature, I redirect the page to itself:
def enable @user.enable redirect_to agent_profile_path(@user) end def disable @user.disable redirect_to agent_profile_path(@user) end
remote: true I can totally remove the redirection and now the file looks cleaner:
def enable @user.enable end def disable @user.disable end
But now the problem is that Rails is expecting a template of JS file but its not there. Hass showed me that I can do
head :ok so the browser will get 200 request instead of 404 error. That means the request has been made but on user point of view, there's nothing is render so she has no clue if the request was successful or not.
So we added a
$("#profile_action").html("<%=j render 'enable_agent' %>");
Now we need the
It's really unbelievable how much stuffs I could learn in only half a day of doing one feature.