我正在处理当前项目中的嵌套表单,并发现了一个非常简单的博客帖子,它帮助了我很多http://iroller.ru/blog/2013/10/14/nested-model-form-in-rails-4/。问题是我似乎无法更新事件(这应该反过来创建嵌套的答案)。我今天大部分时间都在努力解决这个问题,而且我无法取得任何实际进展。
错误
undefined local variable or method `event_params' for #<EventsController:0x007f9847af6d10>
谢谢你们这些愚蠢的问题。如果您想了解更多信息,请告诉我。
模型
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
end
def event_params
params.require(:event).permit(:owner_id,
questions_attributes: [:poll, :event_id],
answers_attributes: [:response, :event_id, :question_id, :user_id])
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 %>
答案 0 :(得分:1)
您没有方法event_params
,或者它可能不适用于课程。在你的情况下,它是另一种方法:
def favorite
@arr = []
cookies.each do |cookie|
@arr.push(cookie)
endhtm
@info = []
for i in 0...@arr.length
if @arr[i][0].index('id')
@info.push(@arr[i][1])
end
@info
end
if @info == []
flash[:error] = "You don't have any events saved yet. Please select events of interest to you."
redirect_to events_path
else
@events = Event.all_events_by_asc_order.where(id: @info)
end
end
def event_params
params.require(:event).permit(
questions_attributes: [:poll, answers_attributes: [:response]])
end
end
应该是:
def favorite
@arr = []
cookies.each do |cookie|
@arr.push(cookie)
endhtm
@info = []
for i in 0...@arr.length
if @arr[i][0].index('id')
@info.push(@arr[i][1])
end
@info
end
if @info == []
flash[:error] = "You don't have any events saved yet. Please select events of interest to you."
redirect_to events_path
else
@events = Event.all_events_by_asc_order.where(id: @info)
end
end
end
def event_params
params.require(:event).permit(
questions_attributes: [:poll, answers_attributes: [:response]])
end
有endhtm
,好像是end
+东西。