Form does not get Submitted via Mechanize - ruby-on-rails

URL = 'http://public.dep.state.ma.us/SearchableSites2/Search_UST.aspx'
agent = Mechanize.new()
agent.get(URL)
form = agent.page.form_with(:action=>/Search_UST.aspx/)
form.submit(form.button_with(:value=>'Search'))
puts agent.page.body
The above snippet is suppose to submit the form and receives search results page. However, the form does not get submitted. Instead of getting results page, I get the form page as if I did not submit the form.
That's the source page I'm trying submit http://public.dep.state.ma.us/SearchableSites2/Search_UST.aspx
Any suggestion on how to overcome this problem?
Thank you

I can see that Search button has a "doPostBack" action in the onclick. So you will need to parse that and do something with it.

Related

page inbetween search results and results

So i have this page called ticket amount which has a bunch of buttons on it.
Wha im wanting is for the user to click the button on the row of the search results and be taken to that page. The user will then click on the amount of tickets they want and be taken to the page that matches the id from the from the row the button was on.
Basically at the moment its going
Search results >>>>> results
And i want
Search results >>>>> ticket amount >>>>> results
I've currently got the first one set up like this
<%= link_to 'Compare', event_path(event.id), class: "btn btn-info" %>
How would i go about making it different? Basically i'm wanting to use the amount of tickets the user selects in the view. So probably best to change the url from this
/events/idnumber
to
events/idnumber/tickets_required=4
Any ideas how to do this?
At the moment the ticket_amount.html only has basic bootstrap buttons inside
What you need to do is to send the params in a redirect_to.
What I would do is a small form for let the user select the tickets_required, the form will point to an action in the controller, within that action I would add a redirect to the search results url sending the param:
#Within your controller action once the user selects the tickets_required
redirect_to event_path(event, tickets_required: params[:tickets_required])
Redirect to the search results url or event_path or whatever, and send the tickets_required param in the url using the param[:ticket_required] coming from the form submission
Take a look to this thread that will guide you about how to send the params in the redirect_to

Rails scraping - submitting a form

I am filling in a form on a page and submitting it.
This should trigger the download of a file.
However, when I try to save the output of the download, I get the source code of the page rather than the file.
My code is:
mechanize = Mechanize.new
mechanize.pluggable_parser.default = Mechanize::Download
page = mechanize.get('http://page.com/')
form = page.forms.first
form.radiobuttons_with(name: 'presence')[0].check
form.source = "btce"
form.label = "BTC/USD"
mechanize.get_file(form.submit).save!('page.csv')
How can I save a file which is downloaded when I submit a form?
Does the file automatically begin downloading once you submit the form?
Submitting a form may return a new page, also new scripts/stylesheets can be loaded. Which possibly explains why your file contains the source code since that's what you're downloading. (Mechanize doesn't throw an error if you download a web page)
For example, I use Mechanize to fill out Google's search form and submit it and save the results to google_search.csv. The new file contains a mixture of the page's source code along with javascript, mySQL, and its stylesheets.
You can dig through the page's source code using Firebug and pinpoint what exactly happens when you submit the form, which can likely be a link that's invoked but you were unaware of.

How can I embed a form (scaffold) into a home page using ROR?

I have created a scaffold called TRADER and gave it the entries
fname:string
lname:string
twitteruser:string
email:string
I want the form visible on the index page for the app user to submit their entry. Upon submission, I want a pop up box to appear saying " Thank you for your submission."
I do not want to show the user what they just submitted, and the DB that holds all the submissions is accessed with authentication only available to me.
Currently the blank form is located in localhost:3000/traders/new
then after submission, the app displays the submitted information.
First, How can I embed the form into the index page ?
Embedding your form
In your index.html.erb, add the following line:
<%= render 'form' %>
Assuming that the form partial is stored under app/view/trader with a name _form.html.erb.
Redirect
In your traders_controller.rb, edit the create action so that the user is not redirected to what the entry they submitted. I assume that there is a line like redirect_to #trader upon a successful save on the database. Instead, use something like redirect_to root_path, or wherever you want the users redirected to.
Pop-up box
In your traders.js.coffee under app/assets/javascripts, add some codes to display alert box on a successful submission of the form. Something like:
$("#your_form").submit(function(event) {
alert("Thank you for your submission.");
});

Asp.Net Mvc remote validation textbox focus issue

I have a single textbox on a form - basically to allow users to change the URL of their site in our mini CMS. We use remote validation to check that the URL is not already taken.
They enter their desired URL and hit the save button. If they do just that and the focus goes from the textbox straight to the submit button - the validation does not happen and the form doesn't submit properly. If they tap into an area of whitespace then the form does.
The issue with the form submitting is that if they tap whitespace we get the name of the submit button posted along with the desired URL - we use the (AcceptParameterAttribute) to allow us to route form submits to the right Action. This uses the submit buttons name attribute to do this. If they don't click whitespace to lose focus on the text box then only the desired URL is posted.
This is a really odd one and it is very annoying. Has anyone seen anything like this before? Is there a way to overcome the problem?
Try adding the following script to the page:
$(":input").live("blur", function () {
$(this.form).validate().element(this);
});
This should cause validation to occur when you click on the submit button. If not, try changing the first line to:
$(":submit").live("click", function () {

Rails and modal jquery dialog form

I'm new to using jquery modal dialog boxes with rails and was wondering how I can :
show a jquery modal dialog box
with 1 field (eg "title")
post this form to a rails controller (via
ajax)
have the modal dialog form
redisplay if field is not filled in
(with normal red css in error field)
Any tutorials or examples welcome.
Using Rails 3 and jQuery. Thanks for your time.
Here's an example of how I'd do it: https://github.com/ramblex/modal-form.
You should be able to:
download the app
run bundle
rake db:migrate
rails s
Go to localhost:3000/articles and the modal form should come up when you click on the 'New article' link.
An error message should be shown when the title field is left empty. Otherwise it should create the article and display it.
Hope it helps.
For modal box I use jQuery Tools.
Once you set that up, next step is to bind an ajax request when the form is submitted (eg: form.submit(function(){ $.post... })) and post the form's data to controller.
Third step is setting up your Rails controller to respond to ajax request (using respond_to block) and render something as response (probably using :layout => false).
If validation failed, you will replace content of your modal box with this response body, or if successful (let's say response was just head :ok), you will display a success message.
I hope this makes sense to you.

Resources