1

我到处都看到过这个问题,但它从来没有解决我的问题。这是我的控制器:

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

有谁知道发生了什么?我找遍了整个地方,我什么也找不到。我想不出任何找不到控制器方法的原因。谢谢!

4

1 回答 1

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| %>

另外,有一个Userhave_manyVacationDay吗?您可能希望更改为资源丰富的路线,并嵌套休假日。

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
于 2013-07-16T04:07:45.583 回答