0

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

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

路线.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

感谢您的任何建议。

4

1 回答 1

1

您还想确保 rails 正在发送这样的 Post 请求:

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

我的猜测是您的网址是正确的,但是将其作为获取而不是帖子发送。正如您从您的路线中看到的那样,索引和创建操作都具有相同的 url,唯一的区别是 get/post。

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

    POST   /ups(.:format)    ups#create  
于 2013-06-14T14:06:32.927 回答