创建方法不提交POST请求

时间:2013-02-08 07:42:31

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我在rails应用程序中有一个控制器,用户可以创建假日请求,当我填写必要的信息时,它似乎没有执行POST请求并提交我的表单。我在RailsPanel中的输出如下:Rails Panel。从此开始,好像它正在执行GET请求时,肯定应该GET然后POST。我相信我已经搞砸了我的创建方法。任何反馈都会非常感谢你!

控制器

class HolidaysController < ApplicationController
  before_filter :authenticate_user!
  before_filter :admin_user, :only => [:index, :update, :edit, :absence]
  before_filter :correct_user, :only => [:delete]

  def new
    @holiday = Holiday.new
    @user = current_user
  end

  def show
    @holiday = Holiday.find(params[:id])
    c_u = current_user
  end

  def create
    @user = current_user
    @holiday = current.holidays.build(params[:holiday])
    @holiday.approver_id = approval_method(current_user, @holiday)
    if @holiday.save
      redirect_to root_path
      flash[:success]= "holiday application sent!"
    else
      render :new
    end
  end

  def myholidays
    @holidays = current_user.holidays.all
  end

  def index
    @holidays = Holiday.all
  end

  def absence
    #show the holidays where the approver id matches the current user id
    #and state = "requested"'

    @user = current_user
    if current_user.role? :administrator
      # a admin can view all current holiday requests
      @holidays = Holiday.all( :conditions => 'state = "requested"')
    else
    #otherwise an admin sees the holiday requests that they are approvers for
      @holidays = Holiday.all(:conditions => ["approver_id = #{current_user.id}", "state = requested"])
    end
  end

  def edit 
    today = Date.today
    @holidays = Holiday.all
    @month = (params[:month] || (Time.zone || Time).now.month).to_i
    @year = (params[:year] || (Time.zone || Time).now.year).to_i
    @shown_month = Date.civil(@year, @month)
    #L51 - Parses the given representation of date and time with the given template
    #and returns a hash of parsed elements.
    @holiday = Holiday.find(params[:id])
  end

 def update
   admin = User.find(current_user.role? :administrator)
   holiday = Holiday.find(params[:id])
   user = User.find(id = holiday.user_id)

   if holiday.update_attributes(params[:holiday])
     if holiday.state == "approved"
       user.absentdays = user.absentdays - (holiday.days_used).to_i
       user.save
     end
     redirect_to absence_path, :notice => "Request updated"
   else 
     render 'edit'
   end
 end

 def destroy 
   Holiday.find(params[:id]).destroy 
   redirect_to root_url, :notice => "Request deleted"
 end

 private 

 def current_user?(user)
   user == current_user
 end 

 def admin_user
   redirect_to dashboard_path, :notice => "You must be an admin to do this!" unless current_user.role? :administrator
 end

 def signed_in_user 
   redirect_to login_path, notice: "Please sign in." unless signed_in? 
 end

 def correct_user 
   @user = current_user 
   redirect_to dashboard, notice: "You are not the correct user." unless current_user?(@user) or current_user.role? :administrator
 end

 def approval_method(current_user, holiday_to_approve)
   found = false 
   days = holiday_to_approve.days_used
   user = current_user 
   approver = user.role? :administrator

   until found == true 
     #Admins should be automatically approved and have no approvers 
     if approver == nil 
       holiday_to_approve.state = "approved"
       #if user absent days is equal to absent days - day and convert to integer
       user.absentdays = user.absentdays - (days).to_i
       user.save

       found = true 
     else 
       redirect_to dashboard_path, :notice => "Request complete"
     end 
     break if found == true 
     end 
   end
end

假期/ show.html.erb

<form class="form">
<p>You have<b><%= @user.absentdays %> days of holiday left.</b></p>
<%= form_for @holiday do |f| %>
    <% if @holiday.errors.any? %>
        <div>
          <h2>Form is invalid</h2>
          <ul>
            <% for message in @holiday.error.full_messages %>
                <li><%= message %></li>
            <% end %>
          </ul>
        </div>
    <% end %>
    Select the dates required for absence<br>
    Start: <%= datepicker_input "holiday", "start_at", :minDate => 0, :dateFormat => "yy-mm-dd" %><br>
    End: <%= datepicker_input "holiday", "end_at", :minDate => 0, :dateFormat => "yy-mm-dd" %>

    <br><br>

    Please select the type of absence you require<br>
    <%= f.collection_select :type_id, Type.all, :id, :name, :prompt => "Select absence type" %>
    <br><br>
    <%= f.text_field :description %>
    <br><br>

    <%= f.submit "Submit Request", :class => "submit" %>
<% end %>
</form>

new.html.erb

<% provide(:title, 'apply for absence') %>

  <p>You have <b><%= @user.absentdays %></b> days of holiday time left.</p>
  <%= form_for @holiday do |f| %>
      <% if @holiday.errors.any? %>
          <div class="error_messages">
            <h2>Form is invalid</h2>
            <ul>
              <% for message in @holiday.errors.full_messages %>
                  <li><%= message %></li>
              <% end %>
            </ul>

      <% end %>
      Select the dates required for absence<br>
      start: <%= datepicker_input "holiday","start_at", :minDate => 0, :dateFormat => "yy-mm-dd" %><br>
      end: <%= datepicker_input "holiday","end_at", :minDate => 0, :dateFormat => "yy-mm-dd" %>
      <br><br>

      Please select the type of absence you require<br>
      <%= f.collection_select :type_id, Type.all, :id, :name, :prompt => "Select absence type" %>
      <br><br>
      Please provide a short description of the nature of your absence (if applicable)<br>
      <%= f.text_field :description %>
      <br><br>

      <%= f.submit "submit" %>

  <% end %>

</div>

1 个答案:

答案 0 :(得分:0)

原因是,您在holidays/show.html.erb但未在holidays/new.html.erb中填写表单。

根据rails惯例,如果在 new.html.erb 中提交表单,则默认情况下会调用该特定控制器的 POST 方法。

但由于您的文件为show.html.erb,因此您必须在form_for中明确定义 POST 方法。

form_for @holiday , :url => { :action => :create }, :html => { :method => "post"} do |f|