2014-09-02 14 views
0

我正在處理當前項目的嵌套窗體,並發現一個非常簡單的博客文章,幫助我很多http://iroller.ru/blog/2013/10/14/nested-model-form-in-rails-4/。問題是,我似乎無法更新事件(這應該反過來創建嵌套的答案)。我今天大部分時間都在努力解決這個問題,而且我還沒有取得任何實際進展。未定義的局部變量或方法`event_params'錯誤與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

那麼,在同一個控制器中的'event_params'方法? – zishe 2014-09-02 02:48:18

+0

@zishe yep thats that correct。我會更新這個問題來反映這一點。 – cwmacken 2014-09-02 02:50:45

+0

但它不會解決錯誤,我想你錯了,只是複製一個鬃毛到另一個,可以肯定。 – zishe 2014-09-02 02:54:49

回答

1

您沒有方法event_params,或者它可能不適用於課程。你的情況是在另一種方法:

def favorite 
    @arr = [] 
    cookies.each do |cookie| 
    @arr.push(cookie) 
    endhtm 
    @info = [] 

    for i in [email protected] 
     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 [email protected] 
     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 +東西。

相關問題