普通网友 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀