Ruby Mechanize Gem find fields without names - ruby-on-rails

I’m trying to use the mechanize gem to scrape a page that is behind a login. However for the site i am using, they don’t name their username or password fields in the html. I’ve searched the Mechanize documentation, but i cannot fins the code to enter textin a field without using the field name. Is there a way to find the 5th element on the page, or the first text box?

If you have liberty to use other gem then why don't you try using Nokogiri Gem.
It is very flexible where you can use either css selector OR can use XPath selector to search specific elements.

You can use
agent = Mechanize.new
page = agent.get(<yourpage>)
form = page.forms[<index of form>]
form.fields[<index of field>].value = '<your value>' # setting the values
form.fields[<index of field2>].value = '<your value2>'# setting the values
page2 = form.submit # submitting the form

Related

How to generate an editable document using prawn

I'm new to Ruby on rails. I work on Rails 5. I have a requirement of generating PDF document on click of a button with the details in the database, which I achieved using PRAWN gem. Now there is a requirement that On click of a button I should get an option, in which I should be able to select whether to generate a document in PDF or editable format(DOCX or any other format).
Can we achieve this with PRWAN gem in rails 5?
Please suggest me if there are any other options available.
The gem Prawn works only the pdf.
I belive that all you need is the gem htmltoword
Usage:
require 'htmltoword'
# Configure the location of your custom templates
Htmltoword.config.custom_templates_path = 'some_path'
my_html = '<html><head></head><body><p>Hello</p></body></html>'
document = Htmltoword::Document.create(my_html, word_template_file_name)
file = Htmltoword::Document.create_and_save(my_html, file_path, word_template_file_name)

Support Markdown editing within rails app

I allow my users to create assignments and distribute them to their employees. Assignments have a attribute called description. Instead of users filling out a plain text field to create a description I want to give them a editable markdown supported box to fill in. exactly like the one I'm filling out now. I've never built anything like this out before, but I'm wondering if there is a Ruby gem or plugin that will help me out with this?
I can recommend you SimpleMDE javascript markdown editor. In this case you don't need a ruby gem for markdown because SimpleMDE can generate html version for you. You need to just save in your database both versions - markdown and html.
We have table assignments with two fields description to keep a markdown version and description_html to keep a html version of a question. As far you create common rails assignments' form and bind simplemde instance to description textarea. For description_html create hidden field tag:
<%= f.hidden_field :description_html %>
<%= f.textarea :description %>
On any change simplemde will save a html version to description_html hidden field:
var simplemde = new SimpleMDE({ element: $("#MyID")[0] });
simplemde.codemirror.on("change", function(){
# set a html to a hidden field
$('#description_html_id_CHANGE_IT').val(simplemde.getHtmlValue());
});
I suggest you use github's gem for that: https://github.com/github/markup
First install a couple of gems:
github/markup
html-pipeline
nokogiri
nokogiri-diff
Then try the following:
require "github/markup"
require 'html/pipeline'
require 'nokogiri'
require 'nokogiri/diff'
filename = ARGV.first
puts GitHub::Markup.render(filename, File.read(filename)).strip.force_encoding("utf-8")

Dynamic Search Field in Rails

I want to build a search field that give suggestions based on what the user is typing. Like the wikipedia search bar.
It should only query for the title names (not implementing any complex algo). What is the best way to do this with rails: gem and from scratch?
Use select2 jquery plugin. Its documentation is pretty cool as well. Visit https://select2.github.io/examples.html . Pass your collection to your form select box and call the class of it with select2. It will suggest the user according to the input.

Rails: Including the '#' as part of a username link when using Twitter Text gem

I'm currently using the (frankly amazing) Twitter Text gem to automatically set up links within a tweet's status for a Twitter widget I'm building.
However, when using the auto_link functionality on usernames (i.e. #adamt), it drops the '#' from the link text, outputting something like:
#adamt
Does anyone know of a simple way to include in the '#' inside that link using the Twitter Text gem?
You need to use the :username_include_symbol => true option (from code).

How can I insert a ruby form into Redcloth-modified HTML in Rails?

I have a page where I use RedCloth to use markdown for the text and images. I want to be able to place a form somewhere in the text by putting the string [Form] and my application does a substitution to replace [Form] with the ruby code to execute a Rails form.
How do I do this? I currently executve the RedCloth-ed text with a "<%=h %>" so I don't know how to substitute a string with the Rails form code?
Thanks!
For example, in my view, I currently display a mp3 player using a gem and its helper:
5 #landing_page
6 #message
7 = mp3_player #landing_page.mp3.url unless #landing_page.mp3_file_name.blank?
8 = #redcloth_landing_page
But I actually want more flexibility in terms of being able to place this mp3 player somewhere within the redcloth document by using a substitution string {mp3}.
I'd like where the {mp3} is placed to be where the mp3 player shows up.
First of all I think i understood your question correctly :D,
If you want to add a form (with html tags) as string and wish to function it as a normal form you should look in to template language like 'liquid' (https://github.com/tobi/liquid/). and it works with 'RedCloth' too
cheers
sameera

Resources