tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.

Join the forum, it's quick and easy

tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.
tutorial net
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
Admin
Admin
Posts : 207
Join date : 2017-11-11
Age : 33
Location : algeria
http://www.tutorial-net.com

ruby- Boolean if else statement not working in view Empty ruby- Boolean if else statement not working in view

Mon Apr 02, 2018 6:18 pm
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:
Code:
<%= 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:
Code:
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

1 Answer
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:
Code:
def index
  @job_running = true
  @savedHTML = ScrapedPage.all
end
Back to top
Permissions in this forum:
You cannot reply to topics in this forum