0

I want a submit button for a form to not appear in a view if a boolean is true. @job_running is set to true in my controller by default for testing reasons and my submit button is always visible.

Edit: I have changed my code and now I set the boolean in a method I call but the issues still persists.

Here is my form from my view:

  <%= form_for @when, :url => {:controller => "page_scraper", :action => "set_schedule"} do |f| %>
      <span>Every:</span>
      <%= select_tag 'hour', options_for_select(0..50) %>
      <%= select_tag 'date', options_for_select(["s","m","h","d","w","mo","y"] ) %>
      <% if @job_running == true %>
        <%= @test%>
      <% else %>
        <%=f.submit "Schedule" %>
      <% end %>  
  <% end %>
  <%= button_tag(id: 'delete_schedule', type: 'button') do %>
      <%= content_tag(:div, 'Delete Schedule') %>
  <% end %>

Metheod where I set the variable in my controller:

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

  def scrape
    @url = watched_link_params[:url].to_s
    puts "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOG #{@url}"
    @page = Nokogiri::HTML(open(@url))
    coder = HTMLEntities.new
    @encodedHTML = coder.encode(@page)
    create(@url,@encodedHTML)
  end

  def index     
    @savedHTML = ScrapedPage.all
  end

  def show
    @savedHTML = ScrapedPage.find(params[:id])
  end

  def new
    @savedHTML = ScrapedPage.new
  end

  def create(url, scraped_html)
    saved_html = ScrapedPage.create(domain: url, html: scraped_html, css: '', javascript: '')

    if saved_html.save
      puts "ADDED TO THE DATABASE"
      redirect_to(root_path)
    else
      puts "FAILED TO ADD TO THE DATABASE"
    end
  end

  def edit
  end

  def upadate
  end

  def delete
    @savedHTML = ScrapedPage.find(params[:id])
  end

  def destroy
    @savedHTML = ScrapedPage.find(params[:id])
    @savedHTML.destroy
    redirect_to(root_path)
  end

  def compare
    coder = HTMLEntities.new

    @domain  = params[:domain].to_s
    puts "DOMAIN------------------------------------#{@domain}"
    puts "Param1------------------------------------#{params[:version_one]}"
    puts "Param2------------------------------------#{params[:version_two]}"
    puts "Param3------------------------------------#{params[:change_type]}"

    version_one = ScrapedPage.select("html").find_by(domain: @domain, created_at: params[:version_one]).html
    version_two = ScrapedPage.select("html").find_by(domain: @domain ,created_at: params[:version_two]).html

    test1 =  Nokogiri::HTML(coder.decode(version_one))
    test2 =  Nokogiri::HTML(coder.decode(version_two))

    if params[:change_type] == "business"
      compared_code = Diffy::Diff.new(test1.xpath("//div"), test2.xpath("//div")).to_s(:html).html_safe
      #concatenate multiple changes here for a change type
    elsif params[:change_type] == "developers"
      @script_changes = Diffy::Diff.new(test1.xpath("//script"), test2.xpath("//script")).to_s(:html).html_safe 
      @meta_changes = Diffy::Diff.new(test1.xpath("//meta"), test2.xpath("//meta")).to_s(:html).html_safe
      @changes = Diffy::Diff.new(test1.xpath("//meta"), test2.xpath("//meta")).to_s(:html).html_safe  
    end
  end

  def watched_link_params
    params.require(:default).permit(:url)
  end

  def compare_params
    params.require(:domain).permit(:domain)
  end

  def set_schedule
    puts @scrape_schedule = "#{params[:hour]}#{params[:date]}"
    schedule("in", @scrape_schedule, "cunt")
  end

  require 'rufus-scheduler'
  @job_id = nil

  @test = "NO!"
  SCHEDULER = Rufus::Scheduler.new

  def schedule(cmd, tim, msg)
    @job_running = true
    SCHEDULER.send(cmd, tim) do |job|
      now = Time.now
      now = "#{now.strftime('%Y-%m-%d %H:%M:%S')}.#{sprintf('%06d', now.usec)}"
      puts("#{now} : #{msg.inspect} : (#{job.id})")
      @job_id = job.id
    end 
  end

  def unschedule(_, job_id, _)
    if (@job_runing)
      SCHEDULER.unschedule(job_id)
      @job_id = nil
    end
  end
end

I know the code is messy I am just trying to see if works then I will clean it up

10
  • Possibly @job_running is truthy but not actually true. Try changing to if @job_running instead of @job_running == true. Also, post relevant controller code where you are setting the value of @job_running Commented Apr 2, 2018 at 15:21
  • I only have @job_running == true because because if @job_running is not working either. As for the where i set the boolean its just at the beginning of the file after the require section so i don't think its worth to post the entire controller. Its just @job_running = true. Commented Apr 2, 2018 at 15:27
  • 2
    Well, it seems that the variable @job_running is set in the wrong place, thus in the view it has nil value. Set the variable within the action that renders the view. Commented Apr 2, 2018 at 15:34
  • 3
    @Padu: so, at the time you render this form, from action index, variable @job_running is not yet set and has default value nil. Which is why you always get into the else clause. Commented Apr 2, 2018 at 16:06
  • 1
    You need to set the instance variable in the action that is called when you display your form, not in the action that is called when you submit your form. It sounds like you need to set it in your index method. Commented Apr 2, 2018 at 16:10

1 Answer 1

2

You must set the @job_running variable inside the action that renders the view; for instance, if the action is index, it will look like this:

def index
  @job_running = true 
  @savedHTML = ScrapedPage.all
end
Sign up to request clarification or add additional context in comments.

2 Comments

But what if the job isn't actually running?
@SergioTulentsev Using @job_running = true is only for the OP to test (as stated in the question), but it should be replaced with the appropriate logic.

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.