表单提交按钮是调用控制器的索引功能

时间:2013-06-13 20:31:39

标签: ruby-on-rails

作者构建了一个UPS脚手架和rails,将模型创建为up而不是ups。我已经破解了一些解决方案以使其工作,但是当从新表单调用时,我无法使create方法工作。 Rails调用索引函数,不会创建新对象。

ups_controller.rb

def create
    @up = Ups.new(params[:up])

    respond_to do |format|
      if @up.save
        format.html { redirect_to @up, notice: 'Up was successfully created.' }
        format.json { render json: @up, status: :created, location: @up }
      else
        format.html { render action: "new" }
        format.json { render json: @up.errors, status: :unprocessable_entity }
      end
    end
  end

def new
  @up = Ups.new

   respond_to do |format|
     format.html # new.html.erb
     format.json { render json: @up }
   end
end

UPS / form.html.erb

<%= form_for(@up) do |f| %>

<table class="table table-striped table-bordered">
        <tr>
            <th>name</th>
            <td><%= f.text_field :name %></td>
        </tr>
        <tr>
            <th>attr</th>
            <td><%= f.select :attr_id, options_from_collection_for_select(Attr.order(:name).where(:major => true).all, "id", "name", @up.attr_id) %></td>
        </tr>
        <tr>
            <th>Action</th>     
            <td><%= f.submit "Submit New UPS" %></td>
        </tr>
    </table>
<% end %>

的routes.rb

  resources :ups

  match 'ups/create' => 'ups#create'
  match 'ups/index' => 'ups#index'
  match 'pdus/link_all_ups' => 'pdus#link_all_ups'

以下是调用ups_controller的新方法的按钮:

<%= link_to 'Enter New UPS', new_ups_path, :class => "btn btn-success pull-right" %>

佣金路线

                                 ups GET    /ups(.:format)                                          ups#index
                                     POST   /ups(.:format)                                          ups#create
                              new_up GET    /ups/new(.:format)                                      ups#new
                             edit_up GET    /ups/:id/edit(.:format)                                 ups#edit
                                  up GET    /ups/:id(.:format)                                      ups#show
                                     PUT    /ups/:id(.:format)                                      ups#update
                                     DELETE /ups/:id(.:format)                                      ups#destroy
                           ups_index        /ups/index(.:format)                                    ups#index

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

您还想确保rails发送一个Post请求,如下所示:

<%= form_for @up, :url=> ups_path, :method => :post do |f| %>

我的猜测是你的网址是正确的,但是将其作为get而不是帖子发送。从路径中可以看出,索引和创建操作都具有相同的URL,唯一的区别是get / post。

ups GET    /ups(.:format)    ups#index

    POST   /ups(.:format)    ups#create