Ask A Question

Notifications

You’re not receiving notifications from this thread.

Current_account selection

Stan X asked in General

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).

Reply

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

Reply

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).

Reply

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).

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 76,990+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.