Ruby表单提交错误的控制器

时间:2018-02-12 14:54:05

标签: ruby-on-rails ruby forms

我有两种形式的视图,我正在尝试提交一个表单,其中包含指向具有操作的控制器的链接。但是,当我尝试将其提交给它时,请在我的其他控制器中查找操作。我尝试以不同的方式使用参数,但它不起作用。我知道我的路径文件有问题,但我不明白,因为我是Rails的新手。如果它有帮助我也使用Rails 5.

编辑:第二种形式是失败的形式。

错误:

No route matches {:action=>"scrape", :controller=>"links_to_be_scraped"}

查看:

<%= form_for WatchedLink.new, url: {action: "create"}, html: {class: "links_to_be_scraped"} do |f| %>
  <%= f.text_field (:link) %>
  <%= f.submit "Save" %>
<% end %>
<% @url = 'default' %>
<%= form_for @url, url: {action: "scrape"}, html: {class: "page_scraper"} do |f| %>
  <%= f.text_field (:url) %>
  <%= f.submit "Scrape" %>
<% end %>

控制器:

class PageScraperController < ApplicationController
    require "nokogiri"
    require "open-uri"
    require "diffy"
    require 'htmlentities'

    def scrape

        @url = params[:url]

        page = Nokogiri::HTML(open(@url)).to_s
        coder = HTMLEntities.new
        encodedHTML = coder.encode(page)
        puts page

     end

end

的routes.rb

Rails.application.routes.draw do
  root 'links_to_be_scraped#index'

    resources :page_scraper do

        member do
            get :scrape
        end
    end 

    resources :links_to_be_scraped do

        member do
            get :delete
        end
    end



end

路由控制台

       Prefix Verb   URI Pattern                               Controller#Action
                      root GET    /                                         links_to_be_scraped#index
       scrape_page_scraper GET    /page_scraper/:id/scrape(.:format)        page_scraper#scrape
        page_scraper_index GET    /page_scraper(.:format)                   page_scraper#index
                           POST   /page_scraper(.:format)                   page_scraper#create
          new_page_scraper GET    /page_scraper/new(.:format)               page_scraper#new
         edit_page_scraper GET    /page_scraper/:id/edit(.:format)          page_scraper#edit
              page_scraper GET    /page_scraper/:id(.:format)               page_scraper#show
                           PATCH  /page_scraper/:id(.:format)               page_scraper#update
                           PUT    /page_scraper/:id(.:format)               page_scraper#update
                           DELETE /page_scraper/:id(.:format)               page_scraper#destroy
delete_links_to_be_scraped GET    /links_to_be_scraped/:id/delete(.:format) links_to_be_scraped#delete
 links_to_be_scraped_index GET    /links_to_be_scraped(.:format)            links_to_be_scraped#index
                           POST   /links_to_be_scraped(.:format)            links_to_be_scraped#create
   new_links_to_be_scraped GET    /links_to_be_scraped/new(.:format)        links_to_be_scraped#new
  edit_links_to_be_scraped GET    /links_to_be_scraped/:id/edit(.:format)   links_to_be_scraped#edit
       links_to_be_scraped GET    /links_to_be_scraped/:id(.:format)        links_to_be_scraped#show
                           PATCH  /links_to_be_scraped/:id(.:format)        links_to_be_scraped#update
                           PUT    /links_to_be_scraped/:id(.:format)        links_to_be_scraped#update
                           DELETE /links_to_be_scraped/:id(.:format)        links_to_be_scraped#destroy

1 个答案:

答案 0 :(得分:2)

由于您不想在表单上提交GET请求,因此您应该将scrape-route定义为POST(或PUT或PATCH,最适合您的需求,澄清,也许这个{{ 3}}和stackoverflow post有帮助)。

因为您当前没有特定的page_scraper对象来提供scrape_page路由,所以完全删除member - 块并将page_scraper路由分别定义为custom-route。像这样:

resources :page_scraper
post 'scrape_page', to: 'page_scraper#scrape'

而不是

resources :page_scraper do
  member do
    get :scrape
  end
end

/page_scraper/:id/scrape VS /page_scraper/scrape

之后,您应该能够将表单标题更改为以下内容:

form_for @url, url: {controller: 'page_scraper', action: 'scrape'} do |f|

因为这是一个非资源路由的情况,那么这是一个很好的地方,可以清楚地了解它应该如何定义:rails-guide