controller
1def index 2require 'selenium-webdriver' 3require 'nokogiri' 4 5options = Selenium::WebDriver::Chrome::Options.new 6options.add_argument('--headless') 7options.add_argument('--disable-gpu') 8 9driver = Selenium::WebDriver.for :chrome, options: options 10 11 12driver.navigate.to 'https://www.justwatch.com/jp/検索?q=ウォーキング・デッド&providers=nfx,hlu,dtv,amp,unx' 13wait = Selenium::WebDriver::Wait.new(timeout: 300) 14wait.until { driver.find_element(xpath: '//div[2]/filter-bar/div[2]/div[2]/div[3]/div[2]/div') } 15doc = Nokogiri::HTML.parse(driver.execute_script('return document.documentElement.innerHTML')) 16 17# 検索結果のタイトルを表示 18doc.xpath('/html/body/div[2]/filter-bar/ng-transclude/core-list/div/div/div[1]/search-result-entry/div/div[2]/div[2]/div/div[1]/div/div/div/div/div[2]/div[1]').each do |node| 19 @result= "結果:" + node.xpath('search-result-entry/div/div[2]/div[1]/a/span[1]').text 20end 21 end 22 23def create 24 @title = Title.new(title_params) 25 26 respond_to do |format| 27 if @title.save 28 format.html { redirect_to @title, notice: 'Title was successfully created.' } 29 format.json { render :show, status: :created, location: @title } 30 else 31 format.html { render :new } 32 format.json { render json: @title.errors, status: :unprocessable_entity } 33 end 34 end 35 end 36 37 38def title_params 39 params.require(:title).permit(:title) 40 end
View
1<h1>Titles</h1> 2 3<%= form_for(@title) do |f| %> 4 <%= f.label :title, 'タイトル' %> 5 <%= f.text_field :title %> 6 7 <%= f.submit '検索' %> 8<% end %> 9 10<%= @result %> 11 12 13 14
scaffoldで簡易的に作っています
ウォーキング・デッドのところをユーザーの入力に応じた検索結果を表示したいのですが、どのようにformを使えばいいのかわかりません。
何卒よろしくお願いいたします!!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/09 10:37
2018/10/09 11:00