Padu
Padu

Reputation: 99

Boolean if else statement not working in view

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

Upvotes: 0

Views: 69

Answers (1)

Gerry
Gerry

Reputation: 10507

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

Upvotes: 2

Related Questions