Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to update data in join table in Rails 4
    primarykey
    data
    text
    <p>I am building an app with Rails 4 using Devise and CanCan. When a new user registers they are not given a default role. The admin has to manually assign it through the admin panel. And that's where I am running into difficulty. I can't figure out how to update the data in the join field. Here's what I have so far.</p> <p>My UsersController:</p> <pre><code> class UsersController &lt; ApplicationController before_filter :authenticate_user! before_action :set_user, only: [:show, :update, :destroy] def index authorize! :index, @user, message: 'Not authorized as an administrator.' @users = User.all end def show end def update authorize! :update, @user, message: 'Not authorized as an administrator.' if @user.update_attributes(user_params) redirect_to users_path, notice: "User updated." else redirect_to users_path, alert: "Unable to update user." end end def destroy authorize! :destroy, @user, message: 'Not authorized as an administrator.' unless @user == current_user @user.destroy redirect_to users_path, notice: "User deleted." else redirect_to users_path, notice: "Can't delete yourself." end end private def set_user @user = User.find(params[:id]) end def user_params params.require(:user).permit(:name, :email, :role_id, :user_id) end end </code></pre> <p>And my role model:</p> <pre><code>class Role &lt; ActiveRecord::Base has_and_belongs_to_many :users, :join_table =&gt; :users_roles belongs_to :resource, :polymorphic =&gt; true scopify end </code></pre> <p>And my user model:</p> <pre><code>class User &lt; ActiveRecord::Base rolify # Include default devise modules. Others available are: # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end </code></pre> <p>As you can see it's pretty much the standard model as generated by Devise.</p> <p>And here's the form I'm using to update the role:</p> <pre><code>&lt;div id="role-options-&lt;%= user.id %&gt;" class="reveal-modal medium" style="display: none;"&gt; &lt;%= simple_form_for user, url: user_path(user), html: {method: :put, class: 'custom' } do |f| %&gt; &lt;h3&gt;Change Role&lt;/h3&gt; &lt;%= f.input :role_ids, collection: Role.all, as: :radio_buttons, label_method: lambda {|t| t.name.titleize}, label: false, item_wrapper_class: 'inline', checked: user.role_ids.first %&gt; &lt;%= f.submit "Change Role", class: "small button" %&gt; &lt;a class="close-reveal-modal" href="#"&gt;Close&lt;/a&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>When I check the new role on the form and submit it, I am redirected to the users page with the message "User updated." However, the user role has not been updated. </p> <p>I'm still fairly new to Rails and just can't figure out exactly what I am doing wrong. If I understand correctly, I need to update the data in the user_roles table. I just can't figure out what I'm doing wrong.</p>
    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. 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