Rails 7如何允许用户从给定的民意调查中创建一个问题的新实例?
我有民意调查,一项民意调查有疑问。这些是模型:
class poll < ApplicationRecord
validates_presence_of :user, :title, :URL, :poll_type, :start_date, :end_date
belongs_to :user
has_many :questions, dependent: :destroy
end
class Question < ApplicationRecord
validates_presence_of :poll_id, :question_type, :title
belongs_to :poll
end
民意调查的架构如下:
create_table "polls", force: :cascade do |t|
t.bigint "user_id", null: false
t.string "title"
t.text "description"
t.text "URL"
t.string "poll_type"
t.datetime "start_date"
t.datetime "end_date"
t.boolean "weighted_voting"
t.boolean "show_results"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_polls_on_user_id"
end
Quesitons
create_table "questions", force: :cascade do |t|
t.bigint "poll_id", null: false
t.string "question_type"
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["ballot_id"], name: "index_questions_on_ballot_id"
end
My Routes.rb
resources :users do
resources :polls do
resources :questions do
resources :options
end
end
end
用户如何从分配给定的民意调查中创建问题的新实例?
当前,问题表中的字段poll_id是空的。我不知道如何填充此ID。民意调查表格从Deaise的current_user.id变量通过表单获取user_id。
还可以通过轨道正确的方式来执行此操作吗?
奖励问题:选项模型的方法是否相同?
I have a poll and a poll has questions. These are the models:
class poll < ApplicationRecord
validates_presence_of :user, :title, :URL, :poll_type, :start_date, :end_date
belongs_to :user
has_many :questions, dependent: :destroy
end
class Question < ApplicationRecord
validates_presence_of :poll_id, :question_type, :title
belongs_to :poll
end
The schema for polls is as follows
create_table "polls", force: :cascade do |t|
t.bigint "user_id", null: false
t.string "title"
t.text "description"
t.text "URL"
t.string "poll_type"
t.datetime "start_date"
t.datetime "end_date"
t.boolean "weighted_voting"
t.boolean "show_results"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_polls_on_user_id"
end
The schema for quesitons
create_table "questions", force: :cascade do |t|
t.bigint "poll_id", null: false
t.string "question_type"
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["ballot_id"], name: "index_questions_on_ballot_id"
end
my routes.rb
resources :users do
resources :polls do
resources :questions do
resources :options
end
end
end
How can the user create a new instance of a question from their assigned given poll?
Currently the field poll_id in the questions form is empty. I can't figure out how to get this id populate. the polls form gets the user_id from devise's current_user.id variable via a form.
Also is doing this via the form the rails correct way of doing it?
Bonus Question: Would the method be the same for the options model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过嵌套表单来做到这一点。
详细信息为
“ nofollow noreferrer”泳池形式。基本上,此表格必须包括民意调查和问题表格字段。您必须使用fields_for帮助者为特定模型创建相关表格。
民意调查表格
轮询控制器中的模型文件中的
允许问题属性
投票控制器新操作
信息
rails文档说:绝不应该将资源嵌套超过1级深。
您只能使用
所有人 解决您的问题保存表单时,需要对轮询控制器进行注入Current_user。您不需要其他资源来提问。您可以在民意调查页面下显示所有问题。
You can do this by nested forms.
Details are here
Firstly you should create new pool form. Basically this form must include Poll and Question form fields. You must use fields_for helper to create related forms for specific model.
poll form
in model file
in poll controller permit the question attributes
poll controller new action
info about routes
Rails docs says: Resources should never be nested more than 1 level deep.
You can solve your problem with only
All you need to do inject current_user to poll controller when saving form. You don't need to another resources for questions. You can show all questions under the poll show page.