ruby on rails-创建不提交POST请求的方法



我在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>

原因是,您的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|

相关内容

  • 没有找到相关文章

最新更新