我希望我不会问一个明显的问题/不会因为这个而投下地狱。我正在使用rails app而且我得到了一个“param is missing or value is empty”错误。
我有一个已经创建的事件和问题,并且我使用嵌套表单允许用户一次回答所有问题。
我正在使用rails 4
模型
class Event < ActiveRecord::Base
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_many :answers
accepts_nested_attributes_for :answers, allow_destroy: true
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
的routes.rb
Rails.application.routes.draw do
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
get 'users/new', to: 'users#new'
post 'users/new', to: 'users#create'
get 'events/favorite', to: 'events#favorite', via:[:get], as: 'favorite'
post 'events/:id' => 'events#update'
get 'answers/new' => 'answers#new'
get 'events/question' => 'events#question'
end
回答控制器
class AnswersController < ApplicationController
def new
@event = Event.find(params[:id])
@answer = Answer.new
end
def show
end
def create
@answer = Answer.new(answer_params)
@answer.save
redirect_to events_path, notice: "Answered Questions"
end
private
def answer_params
params.require(:answer).permit(:response, :question, :event, :user, :event_id, :question_id, :user_id)
end
end
这就是我的问题所在。最初我有一个非常通用的嵌套来自la http://railscasts.com/episodes/196-nested-model-form-revised,但我将form_for切换到@answer,因为这是创建并切换到button_to,因为提交按钮没有写出DB的答案。我相信它试图用@event触发一些东西
<h1>New answers</h1>
<%= fields_for @event do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :questions do |b| %>
<p>
<%= b.text_field :poll%><br />
<%= form_for @answer do |x| %>
<%= x.text_field :response %>
<% end %>
</p>
<% end %>
<%= button_to "New", action: "create"%>
<% end %>
<%= link_to 'Back', answers_path %>
如果您需要更多代码或有任何问题,请告诉我
谢谢!
更新
我已根据此博文http://iroller.ru/blog/2013/10/14/nested-model-form-in-rails-4/
重新编写了我的代码现在我通过事件控制器运行更新,或者至少我正在尝试。 代码如下,现在得到的错误是
未定义的局部变量或方法`event_params'用于#
感谢男女老少抱歉愚蠢的问题
模型
class Event < ActiveRecord::Base
belongs_to :user
has_many :questions
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :user
belongs_to :event
has_many :answers
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
的routes.rb
Rails.application.routes.draw do
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
get 'users/new', to: 'users#new'
post 'users/new', to: 'users#create'
get 'events/favorite', to: 'events#favorite', via:[:get], as: 'favorite'
post 'events/:id' => 'events#update'
get 'answers/new' => 'answers#new'
get 'events/question' => 'events#question'
end
来自events_controller的方法
def question
@event = Event.find(params[:id])
end
def update
@event = Event.find(params[:id])
if @event.update(event_params)
redirect_to events_path, notice: "Answers saved"
else
redirect_to events_question_path, notice: "Answers not saved"
end
questions.erb
<%= simple_form_for(@event) do |f| %>
<%= f.error_notification %>
<%= f.object.name %>
<%= f.simple_fields_for :questions, f.object.questions do |q| %>
<%= q.object.poll%>
<%= q.simple_fields_for :answers, q.object.answers.build do |a|%>
<%= a.text_field :response %>
<% end %>
<%end %>
<%= f.button :submit%>
<% end %>