2013-12-15 44 views
0

当我浏览我的大学/新网页时,HTTP的工作方式与我所期望的相同。这是用于生成表单的服务器输出。Rails 3.2 - 使用GET而不是POST&PUT来创建和更新按钮

enter image description here

现在,当我填写资料,并选择f.submit,我的日志显示的Rails开始GET。

enter image description here

同样的事情,当我试图编辑现有资源情况。这是正确的GET请求来提取资源。

enter image description here

当我选择更新按钮,而不是PUT,我的日志是这样的。 enter image description here

我已经阅读了一些密切相关的SO问题,并意识到我的问题是在我试图将应用程序添加供应商资产时开始的。我还没有得到Bower的正常工作,并且未能使用Rails指南正确地实现我的网站模板,所以也许这就是事情出乎意料的原因。我希望找到与请求一起发送的正确HTTP动词的解决方案。我希望能更好地理解我是否可以在另一个范围内使用凉亭或资产管道解决.js模板问题。或者是添加了我的外部模板文件,导致Rails混合了一些东西。

您的帮助表示赞赏。

这是我的其他相关文件的插图。

# universities_controller.rb 

class UniversitiesController < ApplicationController 
    before_filter :get_university, except: [:index, :new, :create] 

    def index 
    @universities = University.all 
    end 

    def new 
    @university = University.new 
    end 

    def create 
    @university = University.new(params[:university]) 
    if @university.save 
     redirect_to @university, success: 'University added!' 
    else 
     render :new, error: 'There was an error processing your University' 
    end 
    end 

    def show 
    @teams = @university.teams 
    end 

    def edit 
    end 

    def update 
    if @university.update_attributes(params[:university]) 
     redirect_to @university, method: :put, success: 'University updated!' 
    else 
     render :edit, error: 'There was an error updating your University' 
    end 
    end 

    def destroy 
    @university.delete 
    flash.now[:notice] = 'You sure?' 
    redirect_to universities_path 
    end 

    private 
    def get_university 
    @university = University.find(params[:id]) 
    end 
end 

# events_controller.rb 

class EventsController < ApplicationController 
    before_filter :get_teams, only: [:new, :create, :edit] 
    before_filter :get_event, except: [:index, :new, :create] 

    def index 
    @upcoming_events = Event.upcoming 
    @past_events = Event.past 
    end 

    def new 
    @event = Event.new 
    end 

    def create 
    @event = Event.new(params[:event]) 
    if @event.save 
     redirect_to event_path(@event), success: 'Event created!' 
    else 
     render :new, error: 'There was an error processing your form' 
    end 
    end 

    def show 
    end 

    def edit 
    end 

    def update 
    if @event.update_attributes(params[:event]) 
     redirect_to event_path(@event), success: 'Event updated!' 
    else 
     render :edit, error: 'There was an error updating your form' 
    end 
    end 

    def destroy 
    @event.delete 
    redirect_to events_path 
    end 

    private 
    def get_event 
    @event = Event.find_by_id(params[:id]) 
    end 

    def get_teams 
    @teams = Team.all 
    end 
end 

# universities/_form.html.erb 
<div id="main" class="container"> 
    <div id="content" class="content bg-base section"> 
     <div class="ribbon ribbon-highlight"> 
      <ol class="breadcrumb ribbon-inner"> 
       <li><%= link_to 'Home', root_path %></li> 
       <li><%= link_to 'Universities', universities_path %></li> 
       <li class="active" title="universities/new"><em>Form</em></li> 
      </ol> 
     </div><!--/.ribbon.ribbon-highlight--> 
      <h3 class="page-title" style="margin-left: 35.5%;"> 
       <em style="color: #6699CC">Add</em> a University 
      </h3> 
      <form role="form" style="margin-left: 33%;"> 
       <%= form_for @university, html: { class: "form-horizontal" } do |f| %> 
       <% if @university.errors.any? %> 
        <div class="error_messages"> 
         <h3>Please correct the following errors.</h3> 
         <ul> 
          <% @university.errors.full_messages.each do |msg| %> 
           <li><%= msg %></li> 
          <% end %> 
         </ul> 
        </div> 
       <% end %> 
       <div class="form-group"> 
        <%= f.text_field :name, class: 'text_field', 
          placeholder: 'University Name', 
          class: 'form-control input-lg', 
          style: 'width: 50%;' 
          %> 
       </div> 
       <div class="form-group"> 
        <%= f.text_field :url, class: 'text-field', 
          placeholder: 'University Website', 
          class: 'form-control input-lg', 
          style: 'width: 50%;' 
          %> 
       </div> 
       <div class="form-group">     
        <%= f.text_area :address, 
          placeholder: 'University Address', 
          size: '32x3', 
          class: 'form-control input-lg', 
          style: 'width: 50%;' 
          %> 
       </div> 
       <div class="form-group"> 
        <%= f.text_field :city, class: 'text-field', 
          placeholder: 'University City', 
          class: 'form-control input-lg', 
          style: 'width: 50%;' 
          %> 
       </div> 
       <div class="form-group"> 
        <%= f.text_field :state, class: 'text-field', 
          placeholder: 'University State', 
          class: 'form-control input-lg', 
          style: 'width: 50%;' 
          %> 
       </div> 
       <div class="form-group"> 
        <%= f.text_field :zip, class: 'text-field', 
          placeholder: 'University Zip', 
          class: 'form-control input-lg', 
          style: 'width: 50%;' 
          %> 
       </div> 
       <div class="form-group" style="margin-left: 15%;"> 
        <%= f.submit 'Create University!', 
          options = { 
                method: :post, 
                class: 'btn btn-primary' 
               } %> 
       </div> 
       <% end %> 
      </form> 
    </div><!--/#content.content.bg-base.section--> 
</div><!--/#main.container--> 

# events/edit.html.erb 
<div id="main" class="container"> 
    <div id="content" class="content bg-base section"> 
     <div class="ribbon ribbon-highlight"> 
      <ol class="breadcrumb ribbon-inner"> 
       <li><%= link_to 'Home', root_path %></li> 
       <li><%= link_to 'Events', events_path %></li> 
       <li class="active" title="events/new"> 
         <em>Edit Event</em> 
       </li> 
      </ol> 
     </div><!--/.ribbon.ribbon-highlight--> 
     <h6 class="page-title" style="margin-left: 34%;"> 
      <span style="color: #FF3333"><em>Edit</em></span> an Event 
     </h6> 
     <form role="form" style="margin-left: 33%;"> 
      <%= form_for [@team, @event], html: { class: "form-horizontal" } do |f| %> 
      <% if @event.errors.any? %> 
       <div class="error_messages"> 
        <h3>Please correct the following errors.</h3> 
        <ul> 
         <% @event.errors.full_messages.each do |msg| %> 
          <li><%= msg %></li> 
         <% end %> 
        </ul> 
       </div> 
      <% end %> 
      <div class="form-group"> 
       <%= f.text_field :author, class: 'text_field', 
         placeholder: 'Your name', class: 'form-control input-lg', 
         style: 'width: 50%;' %> 
      </div> 
      <div class="form-group"> 
       <div class="pull-left"> 
       <%= f.label 'Home Team' %><br /> 
       <%= f.select :home_team_id, 
         @teams.map { |team| [team.name, team.id] }, 
         class: 'form-control input-lg' %> 
       </div> 
       <div style="margin-left: 37%;"> 
       <%= f.label 'Away Team', style: 'margin-left: 6%; margin-top: -1%;' %><br />  
       <%= f.select :away_team_id, 
         @teams.map { |team| [team.name, team.id] } %> 
       </div> 
      </div> 
      <div class="form-group">  
       <%#= f.label 'Away Team' %><br /> 
       <%#= f.select :away_team_id, 
         @teams.map { |team| [team.name, team.id] } %> 
      </div> 
      <div class="form-group"> 
       <%= f.select :kind, @teams.map { |team| [team.sport_type] }, class: 'form-control input-lg text_field', 
         style: 'width: 50%;' %> 
      </div> 
      <div class="form-group"> 
       <%= f.text_field :home_team_score, class: 'text_field', 
         placeholder: 'Home team score', class: 'form-control input-lg', 
         style: 'width: 50%;' %> 
      </div> 
      <div class="form-group"> 
       <%= f.text_field :away_team_score, class: 'text_field', 
         placeholder: 'Away team away_team_score', class: 'form-control input-lg', 
         style: 'width: 50%;' %> 
      </div> 

      <div class="form-group"> 
       <%= f.text_field :winner, class: 'text_field', 
         placeholder: 'Winning team name', class: 'form-control input-lg', 
         style: 'width: 50%;' %> 
      </div> 
      <div class="form-group"> 
       <%= f.text_area :details, class: 'text_area', 
         placeholder: 'Details about the event', 
         class: 'form-control input-lg', size: '10x3', 
         style: 'width: 50%;' %> 
      </div> 
      <div class="form-group"> 
       <%= f.datetime_select :event_on, 
         class: 'datetime_select form-control input-lg' %> 
      </div> 
      <div class="form-group" style="margin-left: 12.5%;"> 
       <%= f.submit 'Update Event!', 
        options = { 
              method: :put, 
              class: 'btn btn-lg btn-success' 
             } %> 
       <%= link_to :cancel, events_path, class: 'btn btn-sm btn-danger', style: 'margin-top: 2.3%;' %> 
      </div> 
      <% end %> 

     </form> 
    </div><!--/#content.content.bg-base.section--> 
</div><!--/#main.container--> 

# layouts/application.html.erb to demonstrate how I've connected to css and js 

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Sevendaysports</title> 
     <%= stylesheet_link_tag 'application', media: 'all' %> 
     <%= javascript_include_tag 'application' %> 
     <%= csrf_meta_tags %> 
     <%= render partial: 'layouts/summarize_header' %> 
    </head> 
    <body> 
     <%= render 'layouts/nav' %> 
     <%= render 'layouts/flash_messages' %> 
     <%= yield %> 
     <%= render 'layouts/footer' %> 
    </body> 
</html> 

# config/routes.rb 

Sevendaysports::Application.routes.draw do 
    root to: 'static_pages#home', controller: 'static_pages', action: 'home', as: 'root' 

    match 'static_pages/contact', to: 'static_pages#contact', as: :contact 

    devise_for :users 
    resources :articles 
    resources :medias 

    resources :events do 
    resources :articles 
    resources :medias 
    end 

    resources :universities do 
    resources :teams 
    end 

    resources :teams do 
    resources :events 
    end 

    resources :users do 
    resources :medias 
    end 

    resources :teams do 
    resources :medias 
    end 

    resources :events do 
    resources :medias 
    end 

    # match ':controller(/:action(/:id))(.:format)' 
end 

我的资产目前一团糟,但我会根据要求包括他们。

回答

1

退房How do forms with PATCH, PUT, or DELETE methods work?基本上它是一个带有隐藏“_method”字段的GET来避开一些不实现PUT和DELETE的浏览器的限制。

这是在您使用form_for时处理的。你的form_for线路和路线是什么样的?

+0

有问题的form_for在#universities/_form.html.erb下,我尝试用方法替换方法::post:“patch”无济于事。我会看看3.2.14指南,看看它是否覆盖在同一部分。 – sirramongabriel

+0

此外,路由包含在我的代码列表的末尾。 – sirramongabriel

+0

糟糕,我没有注意到滚动条。我看到你在另一个表单中嵌套你的'form_for'。这可能是问题所在。你可以删除包装形式,看看是否有帮助? – JosephL

相关问题