0

ユーザーが休日のリクエストを作成できるレールアプリにコントローラーがありますが、必要な情報を入力すると、POSTリクエストを実行してフォームを送信していないようです。RailsPanel での私の出力は次のとおりです: Rails Panel . これからは、それがGET要求を行っているかのように、確かにそれを実行する必要がありGETますPOST。create メソッドのどこかを台無しにしたと思います。どんなフィードバックも素晴らしいでしょう、ありがとう!

コントローラ

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

1 に答える 1

0

その理由は、あなたはあなたの中にフォームを持っているが、あなたの中にはholidays/show.html.erbないからholidays/new.html.erbです。

Rails の規則によると、フォームがnew.html.erbで送信された場合、デフォルトでその特定のコントローラーのPOSTメソッドが呼び出されます。

しかし、あなたshow.html.erbファイルform_for.

form_for @holiday , :url => { :action => :create }, :html => { :method => "post"} do |f|
于 2013-02-08T08:11:15.313 に答える