Padu
Padu

Reputation: 99

Ruby form submits to the wrong controller

I have view with two forms and I am trying to submit a form with a link to a controller that has an action. But when I try to submit it to it look for the action in my other controller. I tried using parameters in a different way and it did not work. I know something is wrong with my routes file but I do not understand what, as I am new to Rails. Also I am using Rails 5 if it helps.

Edit: the second form is the one that is failing.

Error:

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

View:

<%= 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 %>

Controller:

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

routes console

       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

Upvotes: 1

Views: 486

Answers (1)

W4rQCC1yGh
W4rQCC1yGh

Reputation: 2229

As you don't want to make a GET-request on your form-submit then you should define the scrape-route as POST (or PUT or PATCH, whatever suits your needs the most, to clarify, maybe this stackoverflow post and rails-guide helps).

Because you currently don't have a specific page_scraper object to give to the scrape_page route then remove the member-block completely and define the page_scraper route separately as custom-route. Something like this:

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

instead of

resources :page_scraper do
  member do
    get :scrape
  end
end

(/page_scraper/:id/scrape VS /page_scraper/scrape)

After that you should be able to change your form header to something like this:

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

For it is a case of non-resourceful routing then this is a good place to get a clear picture of how it should be defined: http://guides.rubyonrails.org/routing.html#non-resourceful-routes

Upvotes: 2

Related Questions