Skip to main content

Current_account selection

General • Asked by Stan X

Hi Guys,

In the app I am building, my users have several accounts (though account_users).
For each account they can create, view, update... articles
I would like to have a global selector at the top to pick the account.
Selecting the account would propagate to views, actions....

  1. I tried the current on rails 5.2
    (Rails 5.2 ActiveSupport CurrentAttributes) described in a screencast but was not sure how to modify the params through a dropdown as this is outside of a model form.

  2. As an alternative to a global current_account (which solution I am not capable of doing right know), I was also thinking of creating an additional attribute for the user model like ’selected_account’ as this might ease for changing through a form. but I can see this might not be the way to go if the link is sent across users.

Also, I wanted to avoid carrying the account id in the url as params unless it is much much simpler (or mandatory). but doing this might be a trouble when you share link between users….so I’ll go for the simplest solution (1, 2 or else).


here is my model setup:
app/models/user.rb
has_many :account_users
has_many :accounts, through: :account_users
app/models/account.rb
has_many :account_users
has_many :users, through: :account_users
app/models/account_user.rb
belongs_to :account
belongs_to :user


So far, I have applied the Current.account as described in this other episode ( https://gorails.com/episodes/rails-active-support-current-attributes?autoplay=1 ).

However, not sure how to populate a dropdown idem for shifting this Current.account (meaning to modify script name).

 <select  id="mySelect">
         <% current_user.accounts.order(name: :asc).each do |account| %>
                     <option>
                             <%= link_to(account.name,  params.to_unsafe_h.merge(script_name: account.id))%>
                     </option>
         <% end %>
 </select>

the above does not shift anything.

Not sure this current.company is the direction i should take (option 1 in my question above).


I finally went for 1 with Current.account implementation (a few days banging head). it works about ok....I have a lot of workarounds though...closing this. feel free to comment if you need some input. (PS: be part of the community).


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.