私は、フォーム(「チェックリスト」という名前)を記入してから、彼がしなければならないタスクのリストをユーザが持つことができるアプリケーションを持っています。タスク(「アドバイス」と呼ばれる)は、ユーザーがフォームに入力した回答に関連しています。 たとえば、質問が「あなたは夕食を作ったのですか?ユーザが「いいえ」と答えた場合、アドバイス「夕食を作る」が表示されます。ユーザー、フォーム、およびタスクの関連付けのレール
アドバイスが完了すると、ユーザーは完了とマークできます。アドバイスはすべてのユーザーで同じです。それらは既にadminによって管理者によって作成されています。
ユーザーにチェックリストがあるので、チェックリストはユーザーに属します。
私が遭遇する問題は、ユーザーが完了としてアドバイスをマークすると、すべてのユーザーに対して完了としてマークされます。そうではありません。
この問題を解決する方法がわかりません。ユーザーがアドバイスを作成しないため、アドバイスとユーザー間の関連性「Has-Many」と「Belongs_to」は機能しませんか?
私はレールが新ですので、誰かが助けてくれたらうれしいです。 私はDeviseを使用してユーザーを管理しています。
スキーマ:
ActiveRecord::Schema.define(version: 20160407143608) do
create_table "advices", force: :cascade do |t|
t.string "name"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.boolean "status"
t.string "linkname1"
t.text "link1"
t.text "link2"
t.string "linkname2"
t.text "link3"
t.string "linkname3"
t.integer "ref"
t.boolean "completed"
end
create_table "categories", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "checklists", force: :cascade do |t|
t.boolean "facebook"
t.boolean "twitter"
t.boolean "linkedin"
t.boolean "viadeo"
t.boolean "instagram"
t.boolean "community"
t.boolean "cms"
t.boolean "seo"
t.boolean "crowdfunding"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
add_index "checklists", ["user_id"], name: "index_checklists_on_user_id"
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", null: false
t.datetime "updated_at", null: false
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
end
モデル:
class Advice < ActiveRecord::Base
belongs_to :category
end
class Checklist < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :checklists
end
ビュー:
<%= advice.name %> | <%= link_to "Completed", complete_advices_path(advice), method: :put %>
コントローラ:
def complete
@advice.completed = true
@advice.save
redirect_to root_path
end
ありがとうございますあなたの答えのためにdmetheny。うん、私は結合モデルを生成し、user.rbとadvice.rbに新しい関連付けを追加しました。しかし、実際にはどうしたらいいのか分かりません。あなたは模範を持っていますか? – Ryley38
@ Ryley38はいくつかの編集を加えました。私が必要ならさらに説明することができます。 – toddmetheny
私はまたいくつかの編集を追加しました。これらの詳細をありがとうございます。 はい完了したブール値が "false"の場合、アドバイスを表示することを目指しました。 現在、ビュー内のリンクをクリックすると、ユーザーはブール値を変更する「完了」アクションを呼び出します。だから、このアクションは代わりにuser_adviceレコードを作成する必要がありますか? Newbyの質問に申し訳ありませんが、アクションでそれを作成する適切な方法は何ですか? – Ryley38