无法为UserVacationDaysController找到操作'create'

时间:2013-07-15 20:02:14

标签: ruby-on-rails rails-routing

我到处都看到这个问题,但它从未解决我的问题。继承人我的控制人员:

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

有谁知道发生了什么事?我到处都看了,我找不到任何东西。我想不出为什么找不到控制器方法的原因。谢谢!

1 个答案:

答案 0 :(得分: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 has_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