我一直在抨击这个嵌套表格问题一个星期。我已经阅读了很多帖子并观看了关于这个问题的railscast,但我还没有提出解决方案。我已将代码恢复到此问题的最后(某种程度)工作状态。我希望有人可以从这里指出我正确的方向。
questions.erb
<h1> this is the question page <h1>
<h2>meetup id </h2><%= @meetup_id %>
<br>
<h2>UID</h2><%= @user.uid %>
<br>
<h2>EID</h2><%= @event.id %>
<br>
<%= @questions.each do |q| %>
<%=q.poll%>
<%= form_for (Answer.create), :remote => true do |f| %>
<%= f.hidden_field :event, :value => params[:id] %>
<%= f.hidden_field :user, :value => current_user.id %>
<%= f.text_field :response %>
<%= f.submit %>
<% end %>
<% end %>
控制器部分
def question
@event = Event.find(params[:id])
@questions = Question.all
end
路线
resources :answers
resources :questions
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, controllers: {omniauth_callbacks: "omniauth_callbacks"}
root 'home#index'
get 'users/new', to: 'users#new'
post 'users/new', to: 'users#create'
get 'events/favorite', to: 'events#favorite', via:[:get], as: 'favorite'
resources :events, only: [:index, :show]
get 'events/form/:id' => 'questionss#new'
patch 'events/:id' => 'events#index'
post 'event/update' => 'events#update'
get 'events/question/:id' => 'events#question'
模特
class Answer < ActiveRecord::Base
has_one :user_id
has_one :question_id
accepts_nested_attributes_for :question_id, allow_destroy: true
end
class Event < ActiveRecord::Base
belongs_to :user
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :user_id
has_and_belongs_to_many :answers
belongs_to :event_id
accepts_nested_attributes_for :answers, allow_destroy: true
end
由于
答案 0 :(得分:2)
要解决的问题太多;以下是如何使其发挥作用:
-
<强>路线强>
#config/routes.rb
root 'home#index'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :answers
resources :users, only: [:new, :create]
resources :questions do
resources :answers #-> domain.com/questions/1/answers/new
end
resources :events, only [:index, :new, :show, :update] do
patch ":id", action: :index
collection do
get :favorite
get "question/:id", action: :question
end
end
作为Rails&#39;路由结构is based around resources
,您确实需要确保每个路由都在中声明应用程序中的不同对象。更多信息在这里
-
<强>模型强>
#app/models/answer.rb
Class Answer < ActiveRecord::Base
#fields id | user_id | question_id | other | attributes | created_at | updated _at
belongs_to :user
belongs_to :question
end
#app/models/event.rb
Class Event < ActiveRecord::Base
#fields id | user_id | event | attributes | created_at | updated _at
belongs_to :user
has_many :questions
end
#app/models/question.rb
Class Question < ActiveRecord::Base
#fields id | user_id | event_id | question | attributes | created_at | updated_at
belongs_to :user
belongs_to :event
has_many :answers
accepts_nested_attributes_for :answers, allow_destroy: true
end
进一步参考 - 您应该阅读foreign_keys
关于关系数据库结构的形成。这非常重要,因为当您声明ActiveRecord associations时,您引用了model
,而不是foreign_key
,就像您所做的那样。
-
<强>控制器强>
#app/controllers/answers_controller.rb
Class AnswersController < ApplicationController
def new
@question = Question.find params[:question_id]
@answer = Answer.new
end
def create
@answer = Answer.new(answer_params)
@answer.save
end
private
def answer_params
params.require(:answer).permit(:your, :answer, :params, :question_id)
end
end
我使用answers_controller
的原因是因为accepts_nested_attributes_for
实际上只有在创建新对象时才需要(并希望通过它保存子对象)。
由于您希望独立于Answer
创建Question
,我只需使用nested route在答案创建过程中引用该问题:
-
查看强>
最后,您需要能够在视图中创建Answer
。
以下是:
#app/views/answers/new.html.erb
<%= form_for [@question, @answer] do |f| %>
<%= f.text_field :answer_params %>
<%= f.submit %>
<% end %>
因为您在form_for
帮助程序中使用了嵌套对象,系统将构建到question_answer_path
的路由 - 而不仅仅是回答路径;从而在创建过程中提供对question
对象的访问
面向对象
最后,您需要确定Rails的工作原理
Rails建立在Ruby之上。 Ruby是一种object orientated
语言 - 这意味着你在Ruby中做的所有都围绕&#34;对象&#34;:
转换为Rails,它意味着每个关联,SQL查询&amp;其他功能将直接与对象的结构相关。如果您对如何将功能集成到应用程序中感到困惑,请始终记住系统的面向对象结构