2

I see this question has been ask several times before (i.e. here, here, just to name a few) but I can't get it working with my case. I am using rails 5.

I have in my posts_controller a method called scrape. It creates several object instances.

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def scrape
    destroy_old_data
    @some_var = Scrape.create("something")
  end
  ...
end

I want to create a button in views/posts/index.html.erb for scrape method, but I can't get the button to work. I am so used to use scaffold method that I don't know how to create a custom method.

Some of the methods that I tried:

<%= button_to "scrape", action: "scrape"%>

<%= "scrape me", { :controller => "posts", :action => "scrape"}, class: 'btn btn-primary' %>

<%= button_to "scrape", posts_scrape_path %>

How can I create a scrape button in index.html.erb that triggers scrape method? Do I need to modify my routes as well?

Community
  • 1
  • 1
Iggy
  • 5,129
  • 12
  • 53
  • 87

1 Answers1

4

You need to create a new route as well

post 'scrape' => 'posts#scrape', as: :scrape

Then you have access to scrape_path and scrape_url

button_to 'Scrape', scrape_path
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54