2016-07-30 89 views
-1

其实我想隐藏记录按钮点击暂时与继续运行的平衡。路由错误 - 没有路由匹配[POST]“/ xvaziris/125/hide”

请参考下图所示的屏幕截图以获得更好的理解;

image.png

当我点击 “隐藏” 按钮,我一直运行到以下错误:

没有路由匹配[POST] “/ xvaziris/125 /隐藏”

请参阅屏幕截图如下所示,以便更好地理解;

screenshot.png

正如我提到的方法:让我在局部但是它给我的职务路由错误。

我试图运行耙路线,我可以看到路由存在:

C:\Users\pos1\Desktop\offorgs>bundle exec rake routes 
DL is deprecated, please use Fiddle 
DL is deprecated, please use Fiddle 
      Prefix Verb URI Pattern     Controller#Action 
       root GET /       xvaziris#index 
     **hide_xvaziri GET /xvaziris/:id/hide(.:format) xvaziris#hide** 
      xvaziris GET /xvaziris(.:format)   xvaziris#index 
        POST /xvaziris(.:format)   xvaziris#create 
     new_xvaziri GET /xvaziris/new(.:format)  xvaziris#new 
     edit_xvaziri GET /xvaziris/:id/edit(.:format) xvaziris#edit 
      xvaziri GET /xvaziris/:id(.:format)  xvaziris#show 
        PATCH /xvaziris/:id(.:format)  xvaziris#update 
        PUT /xvaziris/:id(.:format)  xvaziris#update 
        DELETE /xvaziris/:id(.:format)  xvaziris#destroy 
    import_xvaziris POST /xvaziris/import(.:format)  xvaziris#import 
        GET /xvaziris(.:format)   xvaziris#index 
        POST /xvaziris(.:format)   xvaziris#create 
        GET /xvaziris/new(.:format)  xvaziris#new 
        GET /xvaziris/:id/edit(.:format) xvaziris#edit 
        GET /xvaziris/:id(.:format)  xvaziris#show 
        PATCH /xvaziris/:id(.:format)  xvaziris#update 
        PUT /xvaziris/:id(.:format)  xvaziris#update 
        DELETE /xvaziris/:id(.:format)  xvaziris#destroy 
xvaziri_resetfilter GET /xvaziri/resetfilter(.:format) xvaziris#reset_filter 

的routes.rb

Rails.application.routes.draw do 


    root 'xvaziris#index' 

    resources :xvaziris do 
     member do 
      get :hide 
     end 
    end 

    resources :xvaziris do 
     collection { post :import } 
    end 


    get '/xvaziri/resetfilter', to: 'xvaziris#reset_filter' 

end 

xvaziris_controller.rb

class XvazirisController < ApplicationController 
    before_action :set_xvaziri, only: [:show, :edit, :update, :destroy] 


    def index 
     @xvaziris = Xvaziri.find_by hidden: false 
     @xvaziris = Xvaziri.search(params[:search]) 

     respond_to do |format| 
      format.js 
      format.html 
     end 
    end 

    def import 
     Xvaziri.import(params[:file]) 
     redirect_to xvaziris_url, notice: "Xvaziris imported." 
    end 

    def show 
    end 

    def new 
     @xvaziri = Xvaziri.new 
    end 

    def create 
     @xvaziri = Xvaziri.new(xvaziri) 
     if 
      @xvaziri.save 
      flash[:notice] = 'Xvaziri Created' 
      redirect_to @xvaziri 
     else 
      render 'new' 
     end 
    end 

    def edit 
    end 

    def update 
     if @xvaziri.update(xvaziri) 
      flash[:notice] = 'Xvaziri Updated' 
      redirect_to @xvaziri 
     else 
      render 'edit' 
     end 

    end 

    def destroy 
     @xvaziri.destroy 
     flash[:notice] = 'Xvaziri was successfully destroyed.' 
     redirect_to xvaziris_url  
    end 

    def hide 
     @xvaziri = Xvaziri.find(params[:id]) 
     @xvaziri.hidden = true 
     flash[:notice] = 'Xvaziri was successfully hidden.' 
     redirect_to xvaziris_url  
    end 

    def reset_filter 
     xvaziris = Xvaziri.all 
     xvaziris.each do |xvaziri| 
      xvaziri.hidden = false 
     end 
     redirect_to xvaziris_url 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_xvaziri 
     @xvaziri = Xvaziri.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def xvaziri 
     params.require(:xvaziri).permit(:date, :description, :amount, :discount, :paid) 
    end 

end 

index.html.erb

<div class="row"> 

     <div class="col-md-10 col-md-offset-1"> 

      <div class="table-responsive myTable"> 

       <table id = "kola" class="table listing text-center"> 
        <thead> 
        <tr class="tr-head"> 
         <td>Description</td> 
         <td>Amount</td> 
         <td>Discount</td> 
         <td>Paid</td> 
         <td>Balance</td> 
         <td>Button</td> 
        </tr> 
        </thead> 

        <tbody>    
         <%= render @xvaziris %> 
        </tbody> 
        </table> 
       </div> 
      </div> 
     </div> 


<br> 
<br> 
<br> 

<%= link_to "Show all", xvaziri_resetfilter_path %> 

_xvaziri.html.erb

<tr class="tr-<%= cycle('odd', 'even') %>"> 

    <td class="col-3"><%= span_with_possibly_red_color xvaziri.description %></td> 


    <td class="col-1"><%= number_with_precision(xvaziri.amount, :delimiter => ",", :precision => 2) %></td> 

    <td class="col-1 neg"><%= number_with_precision(xvaziri.discount, :delimiter => ",", :precision => 2) %></td> 

    <td class="col-1 neg"><%= number_with_precision(xvaziri.paid, :delimiter => ",", :precision => 2) %></td> 


    <% @balance += xvaziri.amount.to_f - xvaziri.discount.to_f - xvaziri.paid.to_f %> 

    <% color = @balance >= 0 ? "pos" : "neg" %> 

    <td class="col-1 <%= color %>"><%= number_with_precision(@balance.abs, :delimiter => ",", :precision => 2) %></td> 

    <td class="col-1"><%= button_to "Hide", controller: "xvaziris", action: "hide", id: xvaziri, method: :get %></td> 

</tr> 

任何建议者居多。

预先感谢您。

回答

1

我看了看,发现this关于相反问题的线程。根据其中一个答案:

The "link_to" is looking for a /users/new using GET.

The "button_to" is looking for a /users/new using POST

If you create the routes for a controller using:

resources :user By default, /users/new is a GET and not POST so, the second line doesn't find any route.

也许你可以尝试将它改为link_to?

1
<%= button_to "Hide", hide_xvaziri_path(xvaziri), method: :get %> 

你也可以尝试使用link_to并应用一些css到它。

<%= link_to "Hide", hide_xvaziri_path(xvaziri)%> 

默认button_to是POST方法和的link_to是GET方法

1

它显示了隐藏的GET请求的路线,而不是一个POST请求。 hide_xvaziri GET /xvaziris/:id/hide(.:format) xvaziris#hide。尝试手动添加POST请求并检查。

post '/xvaziri/:id/hide', to: 'xvaziris#hide' 
相关问题