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?

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.