普通网友 2016-07-12 19:47
浏览 19

没有路由匹配[POST]“ /”

I'm having some trouble trying to submit an AJAX request to a separate controller in my view.

My form for the AJAX request looks like this: I've also tried sites_search_results_index_path which my routes file says is the proper route.

View:

<div class="pull-left col-md-5">
  <%= form_tag url: sites_search_results, remote: true do %>
    <div class="input-group">
      <%= content_tag :span, class: "input-group-btn" do %>

        <%= button_tag type: "button", class: "btn btn-default dropdown-toggle", data: { toggle: "dropdown" } do %>
          <%= content_tag :span, "", class: "caret" %>
        <% end %>

        <%= content_tag :ul, class: "dropdown-menu" do %>
          <li class="btn btn-default">Domain</li>
          <li class="btn btn-default">CLW</li>
        <% end %>
      <% end %>

      <%= hidden_field_tag "search_by", "Domain" %>
      <%= content_tag :span, class: "input-group-addon", id: "search_view" do %>
        Filter
      <% end %>

      <%= text_field_tag "search_for", "", :class => "form-control" %>
      <%= content_tag :span, class: "input-group-btn" do %>
        <%= button_tag "Search", class: "btn btn-default" %>
      <% end %>

    </div>
  <% end %>
</div>

Controller:

class SitesSearchResultsController < ApplicationController

  def index

    field = "Domain"

    @results = Domain.where()

    respond_to do |format|
      format.js # do ajax action
    end
  end

end

Routes:

resource :sites_search_results do
  post 'index'
end
# also tried with no result:
#post "sites_search_results/index"

rake routes has:

sites_search_results POST   /sites_search_results/index(.:format)            sites_search_results#index
                     POST   /sites_search_results(.:format)                  sites_search_results#create

The result of this is:

undefined local variable or method `sites_search_results'

Any idea what would cause this? I've seen other people have this problem but I have the root path set up already to go to the status page. And I don't see anything that would be attempting to post to root. Let me know if anything else is needed to help. And thanks in advance!

  • 写回答

2条回答 默认 最新

  • weixin_33749242 2016-07-12 19:51
    关注

    Looks like the form_tag url should be:

    <%= form_tag url: sites_search_results_path, remote: true do %>
    

    Or

    <%= form_tag url: sites_search_results_url, remote: true do %>
    

    Run 'rake routes' from the command line and it see if you have 'sites_search_results' or 'sites_search_results_index'. I am guessing the former.

    Also, try changing your route definition from:

    resource :sites_search_results do
      post 'index'
    end
    

    To simply,

    post 'sites_search_results' => sites_search_results#index
    

    Finally, do you need to use a POST? Index methods are usually GETs in which case you'd that post to a get.

    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?