提交失败后的Form_tag路由错误

时间:2017-02-11 12:32:03

标签: ruby-on-rails path controller routing routes

我有一个带有部分表单的索引页面,用于向Package模型提交新记录。我将此表单保留在索引页面中,因此用户在重复此操作时不需要多次离开页面。 在同一页面中,我有一个form_tag fir,用于同一个控制器的多个更新,即packages_controller。 一切正常,除了以下内容:当点击更新按钮,转到表格但不提交我回去(使用浏览器)并尝试选择其他记录进行更新然后我有路由错误:

Routing Error

没有路线匹配[PUT]“/ projects / 47 / orderlines / 18 / packages”

我的索引页面如下所示:

	<% if current_user %>	
		<%= render "packages/form" %>
	<% end %>

<% if @packages.count >= 1 %>

<table class="table table-striped">
  <thead>
    <tr>
		<th> <input type="checkbox" id="selectAll" value="selectAll"></th>
		<th>Packed </th>
      <th>#No.</th>
      <th>Type</th>
      <th>Gross weight</th>
      <th>Length</th>
      <th>Width</th>
      <th>Height</th>
	  <th></th>
	  <th>Container</th>
    </tr>
  </thead>
<%= form_tag edit_multiple_project_orderline_packages_path, method: :get do %>
  <tbody>
    <% for package in @packages %>
		<% if package.packed== true %>
		<% @label_type="success" %>
		<% else %>
		<% @label_type="default" %>
		<% end %>
		<tr>
		 <td><%= check_box_tag "package_ids[]", package.id %></td>
		 <td><span class="label label-<%= @label_type %>"><% if package.packed==true %>Packed<% else %>Unpacked<% end %></span></td>
        <td><%= package.package_no %></td>
        <td><%= package.package_type %></td>
        <td><%= package.gross_weight %></td>
        <td><%= package.length %></td>
        <td><%= package.width %></td>
        <td><%= package.height %></td>
		<% if @orderline.packages.count >= 1  %>
		<td><%= link_to 'Delete', [package.orderline.project, package.orderline, package],
									  method: :delete,
									 	data: { confirm: 'Are you sure?' } %></td>
	
		<td><%= @containers.find(package.container_id).container_id if package.packed %></td>				
		<% end %>
      </tr>
    <% end %>
  </tbody>
</table>
<%= submit_tag "Add to container", class: "btn btn-primary" %>
<% end %>
<br />

<%= will_paginate %>
<br>

还有multiple_edit表格

<div class="col-sm-4">
	<%= form_tag update_multiple_project_orderline_packages_path, method: :put do %>
		<ul>
			<% @packages.each do |package| %>
			<li>
				<%= hidden_field_tag "package_ids[]", package.id %>
				<%= package.package_no %>
				<%= package.container_id %>
					<% package.errors.full_messages.each do |msg| %>
					<li><%= msg %></li>
					<% end %>
			</li>
			<% end %>
		</ul>
		<%= fields_for :package do |f| %>
			<div class="field">
				<%= f.label :package_no %><br />
				<%= f.text_field :package_no, :class => "form-control" %>
			</div>
			<br />
			<div class="field">
				<%= f.label :container_id %><br />
				<%= select_tag 'package[container_id]', options_from_collection_for_select(@project.containers, 'id', 'container_id', default_blank: true), prompt: "- Select container -", :class => "form-control" %>
			</div>
			<br />
			<div class="field">
			      <%= f.label :packed %><br />
			      <%= f.select :packed, [["Packed", true], ["Unpacked", false]],{ prompt: "- Packing -"},{ :class => "form-control" } %>
			</div>
		<% end %>
		<div class="actions">
			<br />
			<%= submit_tag "Update", :class => "btn btn-primary" %>
		</div>
	<% end %>
</div>

包控制器edit_multiple操作:

  
  def edit_multiple
    @project = Project.find(params[:project_id])
    @packages = Package.find(params[:package_ids])
  end
  
  def update_multiple
    @packages = Package.find(params[:package_ids])
    @packages.reject! do |package|
      package.update_attributes(package_params.reject { |k,v| v.blank? })
    end
    if @packages.empty?
      redirect_to project_orderline_packages_url
    else
      @package = Package.new(package_params)
      render "edit_multiple"
    end
  end

packages_controller创建动作:

def create
    project = Project.find(params[:project_id])
    orderline = project.orderlines.find(params[:orderline_id])
      @package = orderline.packages.new(package_params)
      @package.save
    
    
    if @package.save
      flash[:success] = "Package(s) was successfully added."
      redirect_to :back
    else
      render 'new'
    end

我的路线:

resources :projects do
  resources :containers
  resources :orderlines do
     resources :packages do 
       collection do 
         put :packed
         get :edit_multiple
         put :update_multiple
       end
     end
   end
end

我刚刚在这里添加了我的路线:

  • edit_multiple_project_orderline_packages_path GET /projects/:project_id/orderlines/:orderline_id/packages/edit_multiple(.:format) 包#edit_multiple update_multiple_project_orderline_packages_path PUT /projects/:project_id/orderlines/:orderline_id/packages/update_multiple(.:format)
    包#update_multiple project_orderline_packages_path GET /projects/:project_id/orderlines/:orderline_id/packages(.:format) 包#指数 POST /projects/:project_id/orderlines/:orderline_id/packages(.:format) 包#创建 new_project_orderline_package_path GET /projects/:project_id/orderlines/:orderline_id/packages/new(.:format) 包#新 edit_project_orderline_package_path GET /projects/:project_id/orderlines/:orderline_id/packages/:id/edit(.:format) 包#编辑 project_orderline_package_path GET /projects/:project_id/orderlines/:orderline_id/packages/:id(.:format) 包#秀 PATCH /projects/:project_id/orderlines/:orderline_id/packages/:id(.:format) 包#更新 PUT /projects/:project_id/orderlines/:orderline_id/packages/:id(.:format) 包#更新 DELETE / projects /:project_id / orderlines /:orderline_id / packages /:id(。:format)

1 个答案:

答案 0 :(得分:0)

您的form_tag代码为ir.vian_web.khezrpour.hadi.pezeshk_yab.G cannot be cast to android.app.Activity 我认为应该是update_multiple_project_orderline_packages_path

我不确定我上面的说法,因为您给出了混乱的Rails路线,很难阅读

,您的表单操作似乎进入了update_multiple_project_orderline_package_path(project_id, orderline_id, package_id)控制器 因此请粘贴您的packages#edit_multiple方法,而不是edit_multiple方法

您是使用JavaScript还是纯HTML来实现上述方案?