0

I have a the following form:

 <h2>Add collaborators to the wiki <strong><%= @wiki.title %></strong></h2>

<%= form_for ([@wiki, @collaboration]) do |f| %>
<% @users.each do |user| %>

<p><%= check_box_tag 'user_ids[]', user.id %>
<%= label_tag 'user_ids[]', user.email %> 
<% end %>

<p> <%= f.submit %> <p>
<% end %>

It should do the following, provide the possible the check users => an then all this users should be able to edit this particular form (@wiki)

I therefor created a join table which takes a user_id and wiki_id. If i try to to save the collaborators in through the form it does not seem to work however.

I get this in my rails c

 #<Collaboration id: 1, user_id: nil, wiki_id: 1, created_at: "2015-02-20 10:40:49", updated_at: "2015-02-20 10:40:49">,

So it does not seem to fetch the user.

My controller is set up like this

 class CollaborationsController < ApplicationController

def new 
  @wiki = Wiki.find(params[:wiki_id])
  @collaboration = @wiki.collaborations.new
  @users = User.all
end

def create
  @wiki = Wiki.find(params[:wiki_id])
  #selected users
  @collaboration = @wiki.collaborations.build(user_id: params[:user_id])
   if @collaboration.save
    redirect_to wikis_path, notice: "Wiki shared."
   else
    flash[:error] = "Error creating wiki. Try again."
    render :new
   end
   end

  end

And my schema file looks like this:

create_table "collaborations", force: :cascade do |t|
 t.integer  "user_id"
 t.integer  "wiki_id"
 t.datetime "created_at"
 t.datetime "updated_at"
end

create_table "users", force: :cascade do |t|
 t.string   "email",                  default: "", null: false
 t.string   "encrypted_password",     default: "", null: false
 t.string   "reset_password_token"
 t.datetime "reset_password_sent_at"
 t.datetime "remember_created_at"
 t.integer  "sign_in_count",          default: 0,  null: false
 t.datetime "current_sign_in_at"
 t.datetime "last_sign_in_at"
 t.string   "current_sign_in_ip"
 t.string   "last_sign_in_ip"
 t.datetime "created_at"
 t.datetime "updated_at"
 t.string   "role"
end

 add_index "users", ["email"], name: "index_users_on_email", unique: true
 add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

create_table "wikis", force: :cascade do |t|
 t.string   "title"
 t.text     "body"
 t.boolean  "private"
 t.integer  "user_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
end

add_index "wikis", ["user_id"], name: "index_wikis_on_user_id"

create_table "wikis_and_collaborators", force: :cascade do |t|
 t.integer  "user_id"
 t.integer  "wiki_id"
 t.datetime "created_at"
 t.datetime "updated_at"
end

end

Any thoughts on what goes wrong here?

fuzzyalej
  • 5,903
  • 2
  • 31
  • 49
user3706202
  • 197
  • 1
  • 3
  • 14

1 Answers1

0

Let's say we have params[:user_ids] = [123, 456, 789]

You can say @wiki.user_ids = [123, 456, 789]; @wiki.save and that will make the join records automatically. So, this is actually an update on the wiki object, and your form should be editing the Wiki object too. I would do it like so:

<h2>Add collaborators to the wiki <strong><%= @wiki.title %></strong></h2>

<%= form_for (@wiki) do |f| %>
  <% @users.each do |user| %>

    <p><%= check_box_tag 'wiki[user_ids][]', user.id, @wiki.user_ids.include?(user.id) %>
    <%= label_tag 'wiki[user_ids][]', user.email %> 
  <% end %>
  <p> <%= f.submit %> <p>
<% end %>

This will submit to the WikiController#update action, or the WikiController#create action, depending on whether @wiki is a new record or not. params will be params = {:id => 6, :wiki => {:user_ids => [123, 456, 789]}}, where 6 is an example wiki id.

Following convention, you wouldn't be accessing the CollaborationsController at all, you'd be accessing the WikisController, since it's a Wiki that's being updated. The WikisController#update action would be totally standard:

def update
  @wiki = Wiki.find_by_id(params[:id])
  @wiki.update_attributes(params[:wiki])
  redirect_to wiki_path(@wiki) #or whatever
end
Max Williams
  • 32,435
  • 31
  • 130
  • 197