Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 用解析数据填充rails表单_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails 用解析数据填充rails表单

Ruby on rails 用解析数据填充rails表单,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,我有一个nokogiri脚本,在我的库中设置为一个模块。我的产品表单中还有一个字段,它获取url并将其传递给nokogiri脚本。如何用nokogiri模块解析的数据填充表单字段。本质上,我希望表单中填充解析后的数据,这样用户只需查看产品并将其添加到数据库中 我的模块: module product def get_product url = "" product_page = Nokogiri::HTML(open(url)) im

我有一个nokogiri脚本,在我的库中设置为一个模块。我的产品表单中还有一个字段,它获取url并将其传递给nokogiri脚本。如何用nokogiri模块解析的数据填充表单字段。本质上,我希望表单中填充解析后的数据,这样用户只需查看产品并将其添加到数据库中

我的模块:

    module product
   def get_product
        url = ""
        product_page = Nokogiri::HTML(open(url))
        image_url = product_page.at_xpath('/html/body/div/div[2]/div[4]/div[4]/div/div/div/div').children[3].attributes['href'].text
          title_text = product_page.at_xpath('/html/body/div/div[2]/div[4]/div[4]/div[2]/div[2]/div/form/div/h1').text
        description = product_page.at_xpath('//*[(@id = "SITCC_1column")]//*[contains(concat( " ", @class, " " ), concat( " ", "ItemSectionContent", " " ))]').text.strip!
        curr_item_price = product_page.at_xpath('//*[(@id = "WM_PRICE")]//*[contains(concat( " ", @class, " " ), concat( " ", "camelPrice", " " ))]').text[/[0-9\.]+/]
        base_item_price = product_page.at_xpath('//*[(@id = "WM_PRICE")]//*[contains(concat( " ", @class, " " ), concat( " ", "SubmapPrice", " " ))]').text[/[0-9\.]+/]      
        wm_item_num = url.split("/").last
        id_str = "walmart_#{wm_item_num}"

      end
    end
请原谅我的语法,我想解决你问题的方法是,
@yogi先生:我们在做什么?还在努力让它发挥作用。我想我的路由设置有一些问题,但是非常感谢你的帮助!发布rake路由的输出
 ====================================Module================================            
        module product
                   def get_product(product)
                        product_page = Nokogiri::HTML(open(product.url))
                        product.image_url = product_page.at_xpath('/html/body/div/div[2]/div[4]/div[4]/div/div/div/div').children[3].attributes['href'].text
                        product.title_text = product_page.at_xpath('/html/body/div/div[2]/div[4]/div[4]/div[2]/div[2]/div/form/div/h1').text
                        product.description = product_page.at_xpath('//*[(@id = "SITCC_1column")]//*[contains(concat( " ", @class, " " ), concat( " ", "ItemSectionContent", " " ))]').text.strip!
                        product.curr_item_price = product_page.at_xpath('//*[(@id = "WM_PRICE")]//*[contains(concat( " ", @class, " " ), concat( " ", "camelPrice", " " ))]').text[/[0-9\.]+/]
                        product.base_item_price = product_page.at_xpath('//*[(@id = "WM_PRICE")]//*[contains(concat( " ", @class, " " ), concat( " ", "SubmapPrice", " " ))]').text[/[0-9\.]+/]      
                        product.wm_item_num = url.split("/").last
                        product.id_str = "walmart_#{wm_item_num}"

                        product # returning product after parsing the url and setting other fields.

                      end
                    end

    ====================================View================================


                <%= form_tag url_for(:controller => 'products', :action => 'parse_url') do  %>

                   <p>
                    <%= label_tag :enter_url %> :<br />
                    <%= text_field_tag :url %>
                  </p>   

                  <p class="button"><%= submit_tag "Parse"  %></p>

    ====================================Controller================================

            class ProductsController < ApplicationController
             include MyModule #if your file name is my_module.rb in /lib folder.

              def parse_url
                @product = Product.new
                @product = get_product(@product)
                render ('products/new')
              end

              def create

                @product = Product.new(params[:products]) # the usual way

                if @product.save
                else
                end

              end

            end

    ====================================View================================

                <%= form_for @product, :url => {:controller => 'products', :action => 'create'} do |f| %>

                <p>
                    <%= f.label :image_url %> : <%= f.text_field :image_url %>
                </p>
                <p>
                    <%= f.label :title_text %> : <%= f.text_field :title_text %>
                </p>
                <p>
                    <%= f.label :description %> : <%= f.text_field :description %>
                </p>
                .
                .
                .
                .
                <p class="button"><%= f.submit "Create this product"  %></p>

    ====================================The End================================


Thanks.