Rails 7如何允许用户从给定的民意调查中创建一个问题的新实例?

发布于 2025-01-21 07:54:40 字数 1527 浏览 2 评论 0原文

我有民意调查,一项民意调查有疑问。这些是模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给我一枪 2025-01-28 07:54:40

您可以通过嵌套表单来做到这一点。
详细信息为

“ nofollow noreferrer”泳池形式。基本上,此表格必须包括民意调查和问题表格字段。您必须使用fields_for帮助者为特定模型创建相关表格。

民意调查表格

  <%= form_for @poll do |poll| %>
      Poll name: <%= poll.text_field :poll_name%>
   
    
      <%= fields_for :question, @poll.questions do |question_fields| %>
            Question 1  : <%= question_fields.text_field :question_1 %>
            Answer 1    : <%= question_fields.check_box :answer_1 %>
            Answer 2    : <%= question_fields.check_box :answer_2 %>
            Answer 3    : <%= question_fields.check_box :answer_3 %>
      <% end %>
    
      <%= poll.submit %>
    <% end %>

轮询控制器中的模型文件中的

class Poll < ApplicationRecord
  validates_presence_of :user, :title, :URL, :poll_type, :start_date, :end_date
  belongs_to :user
  has_many :questions, dependent: :destroy
  accepts_nested_attributes_for :questions
end

允许问题属性

questions_attributes: [
          :id,
          :question_text,
          :answer_1,
          :answer_2,
          ...
          :poll_id]

投票控制器新操作

5.times { @poll.questions.build } 

信息

rails文档说:绝不应该将资源嵌套超过1级深。

您只能使用

resources :polls

所有人 解决您的问题保存表单时,需要对轮询控制器进行注入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

  <%= form_for @poll do |poll| %>
      Poll name: <%= poll.text_field :poll_name%>
   
    
      <%= fields_for :question, @poll.questions do |question_fields| %>
            Question 1  : <%= question_fields.text_field :question_1 %>
            Answer 1    : <%= question_fields.check_box :answer_1 %>
            Answer 2    : <%= question_fields.check_box :answer_2 %>
            Answer 3    : <%= question_fields.check_box :answer_3 %>
      <% end %>
    
      <%= poll.submit %>
    <% end %>

in model file

class Poll < ApplicationRecord
  validates_presence_of :user, :title, :URL, :poll_type, :start_date, :end_date
  belongs_to :user
  has_many :questions, dependent: :destroy
  accepts_nested_attributes_for :questions
end

in poll controller permit the question attributes

questions_attributes: [
          :id,
          :question_text,
          :answer_1,
          :answer_2,
          ...
          :poll_id]

poll controller new action

5.times { @poll.questions.build } 

info about routes

Rails docs says: Resources should never be nested more than 1 level deep.

You can solve your problem with only

resources :polls

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文