0

次のフォームがあります。

 <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 %>

次のことを行う必要があります。ユーザーのチェックを可能にする => 次に、このすべてのユーザーがこの特定のフォーム (@wiki) を編集できるようにする必要があります。

そのため、user_id と wiki_id を取る結合テーブルを作成しました。ただし、フォームを使用して共同編集者を保存しようとすると、うまくいかないようです。

私は私のレールでこれを取得します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">,

そのため、ユーザーを取得していないようです。

私のコントローラーはこのように設定されています

 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

私のスキーマファイルは次のようになります。

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

ここで何がうまくいかないかについて何か考えはありますか?

4

1 に答える 1