Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Rather than storing the company_id in the session, you should instead add a randomly generated token column to the company, and get the id by doing Company.find_by_token(session[:token]). If you look at how the current_user method in <a href="http://railscasts.com/episodes/270-authentication-in-rails-3-1" rel="nofollow">this Railscast on authentication</a>, it's the same idea.</p> <p>Edit: Sorry, I misunderstood your question. You should not have a hidden company_id field at all in your view. You should be setting it manually in your create method:</p> <pre><code>@user = User.new(params[:user]) @user.company_id = session[:company_id] </code></pre> <p>And you can protect the company_id from ever being set from the user changing an input name by having company_id protected against mass assignment in the model:</p> <pre><code>attr_protected :company_id </code></pre> <p>See the <a href="http://guides.rubyonrails.org/security.html#mass-assignment" rel="nofollow">rails guide on mass assignment protection</a> for more information. Note: a more common solution is something along these lines:</p> <pre><code>class ApplicationController &lt; ActionController::Base protect_from_forgery def current_company @current_company ||= Company.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] end end class User &lt; ApplicationController def create @user = current_company.users.build(params[:user]) end end </code></pre> <p>UPDATE 2:</p> <p>So you're creating a user and a role, and want to do separate validation on them, this should do what you want.</p> <pre><code>role_params = params[:user].delete :role # Change to the appropriate symbol for your form @user = User.new(params[:user]) role = @user.roles.build(role_params) role.company_id = session[:company_id] if(@user.save and role.user_id = @user.id and role.save) # Might want to just check for valid instead of trying to save ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload