Rails 3.2 - 使用GET而不是POST&创建和更新按钮。放

时间:2013-12-15 22:09:27

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

当导航到我的大学/新页面时,HTTP正如我所期望的那样工作。这是用于生成表单的服务器输出。

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 个答案:

答案 0 :(得分:1)

签出How do forms with PATCH, PUT, or DELETE methods work?基本上它是一个带有隐藏的“_method”字段的GET,以解决某些未实现PUT和DELETE的浏览器的限制。

使用form_for时会处理此问题。您的form_for行和路线是什么样的?