2013-07-15 37 views
1

我在任何地方都看到過這個問題,但它永遠不能解決我的問題。繼承人我的控制器:無法爲UserVacationDaysController找到'創建'動作

class UserVacationDaysController < ApplicationController 
    def new 
    @user = current_user 
    @user_vacation_days = UserVacationDay.new 
    end 

    def create 

    @user_vacation_days = UserVacationDay.create(params[:user_vacation_day]) 
    @user_vacation_days.user = current_user 

    # @user_vacation_days.calculate_work_days 
    # (another param that holds date range will get passed in) 
    # puts @user_vacation_days.errors.inspect 


    if @user_vacation_days.persisted? 
     flash[:notice] = "Request Sent" 
     redirect_to dashboard_index_path 

     request_vacation_days # method from model. model method calls method in employee_mailer 

    else 
     flash[:notice] = "Something went wrong, please try again" 
     render :new 

    end 

    end 

end 

這裏是我的看法(表單)。

<h2>Request Days Off</h2> 

<%= form_for :user_vacation_days, :url => user_vacation_days_path do |f| %> 

    <div><%= f.label "How much time off would you like to take?" %> 
    <%= f.number_field :number_of_days %></div> 

    <div><%= f.label "Argue your case, slave" %> 
    <%= f.text_area :description %></div> 

    <div><%= f.submit "Request Time Off" %></div> 

<% end %> 

的路線我的2種控制器方法

 user_vacation_days POST /user_vacation_days(.:format)  user_vacation_days#create 
    new_user_vacation_day GET /user_vacation_days/new(.:format) user_vacation_days#new 

沒有人有任何想法是怎麼回事?我看遍了各處,我找不到任何東西。我想不出爲什麼控制器方法不會被找到。謝謝!

+0

你可以在你的form_for中添加':method =>'POST''並查看它是否清除了它? –

+0

奇怪..你嘗試重新啓動你的Rails服務器嗎? – Benj

+0

我重新啓動了rails服務器,它工作。謝謝本 – Mike

回答

1

而不是<%= form_for :user_vacation_days, :url => user_vacation_days_path do |f| %>如果使用<%= form_for @user_vacation_days, :url => user_vacation_days_path do |f| %>

而且發生了什麼,做了User have_many VacationDay?您可能需要更改爲足智多謀的路線,並有假期天嵌套。

config/routes.rb 

resources :users do 
    resources :user_vacation_days 
end 

in your new action under UserVacationDaysContoller #may want to rename this to just VacationDays since the nesting implies 

def new 
    @user = current_user 
    @user_vacation_days = @user.vacation_days.build 
end 
+0

用戶做了have_many:vacation_days。我會研究嵌套資源。這似乎是一個好主意 – Mike

相關問題