vendredi 16 septembre 2016

Issues with checkboxes updating join table

Theres a lot of stuff around that is similar but nothing actually addresses this particular circumstance. I would like to preface by saying im still new in the Rails world.

So i have a User table and a Skill table and corresponding models.

I have created a join table to connect the 2 and have a HABTM relationship between the 2.

SKILL MODEL

class Skill < ActiveRecord::Base
  has_and_belongs_to_many :users
end

USER MODEL

class User < ActiveRecord::Base
  has_and_belongs_to_many :skills
end

JOIN TABLE MIGRATION class CreateJoinTableSkillsUsers < ActiveRecord::Migration

  def change
    create_join_table :skills, :users do |t|
       t.index [:skill_id, :user_id]
       t.index [:user_id, :skill_id]
    end
  end
end

ADDSKILL METHOD (In skills controller)

def addSkill
     @user.id = current_user.id
     @skill.id = Skill.find(params[:id])
     params[:user_id, :skill_id]
     redirect_to user_path
end

RELEVANT ROUTE?

put 'skills/addSkill' => 'skills#addSkill'

index.html.erb (skills)

  <% commerceCategory.each do |skill| %>
    <ul>
      <%= button_to skill.title, :method => "addSkill" %>
    </ul>
  <% end %>

Submit Button <%= submit_tag "Update Skills"%>

So basically i want:

a) skill.title to be a checkbox of itself (the name within the checkbox not one alongside it)

b)once its clicked, it will assign that particular skill to the current_user.id via the join table and redirect to the users show page to show the skills that user has.

I have tried so many different things and i cant seem to make it work.

Ive read all over that the has many through is a preferential association especially since i will be wanting to add varying degrees of skill for each skill eventually.

Any guidance would be greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire