Iterating through website links and clicking them - ruby-on-rails

#editLinks = $ie.link(:text => "Edit", :class =>"edit" ).links
#editLinks.each do |l| l.click
end
I have the above code which iterates through a set of links which are called "Edit". When edit is clicked on the webpage, the text boxes become enabled and the user is able to type into them. There are different edit links for different textboxes. However, for some reason, the code is not iterating or giving me any errors. It just finishes the script without clicking any edit links. I've omitted the javascript onlcick stuff to show just the html. All the edit links on the webpage also have different ID's.
<a id="EditLinkButton" class="edit"><strong>Edit</strong></a>

After reading Get Elements By Attributes question, I'd suggest doing something like the following:
#editLinks = $ie.links
#editLinks.each do |link|
if link.attribute_value("class") == "edit" and link.text == "Edit"
link.click
end
end
There's probably a better way of doing this, but it works.
Edit:
After reading your comment below, perhaps something along the lines of this would be better
#edit_ids = %w(your edit link ids seperated by whitespace)
#edit_ids.each do |edit_id|
$ie.link(:id => edit_id).click
# whatever else you're doing
end
So that way you're looking them up in the DOM as and when you need them, rather than storing them and then their references becoming obsolete as the page changes during the test. Worth a shot.

Related

page with params between search and show page

So i have this page that shows a result of events with a button that takes you to the eventid (which has a list of prices)
What im wanting to do is one of my api's is only showing all tickets available (how considerate). So i'm having to adjust to them.
What im wanting (and have made but not put in yet) is to have a page inbetween the search results and the show.html.erb
This page has a bunch of buttons on it, Asking how many tickets your looking for. e.g. 2 tickets for the event.
Now what im wondering is how i can go about this?
Currently the button is linked up like this (on the search results page)
<%= link_to 'Compare', event_path(event.id), class: "btn btn-info" %>
However what im going to want to do is have this button link off to a page called ticket_numbers.html.erb This will sit inbetween these two pages and when a user clicks said button on that page it shall allow me to use the number in the view and related jquery files.
Any help would be great!!
Thanks
Sam
You need to define a route for this new page:
match '/ticket_numbers' => 'controllerName#ticket_numbers'
And to create the file ticket_numbers.html.erb on the corresponding view folder.After this your new page will work.
If you need any controller logic define a method ticket_numbers on the corresponding controller:
class controllerNameController < ApplicationController
def ticket_numbers
end
end

ruby rails print specific elements from multiple partial files residing in a directory

I have a directory filled with partials. I'm looking to list ONLY the first h1 tags in each partial. The methods to accomplish this task could probably be modified to grab other elements as well.
Right now I use ruby to open each file, print out the first few characters, close file, and repeat. My ruby file parsing skills are limiting me. Here's the code I have at the moment:
<% Dir["app/views/partials/show/*.html.erb"].each do |f1| %>
<% aFile = File.open(f1, "r") %>
<% if aFile %>
<% content = aFile.sysread(20) %>
<p><%= content %></p>
<% else %>
<%= "Unable to open file!" %>
<% end %>
<% end %>
I also think I'm opening the entire partial in memory? Wondering If I can just read up until I find my h1 tag then close file and move on? Again I'm only reading first 20 characters because I haven't yet grasped a way to search for the first h1 tag.
I'll make edits as I work through the open, parse, piece... I appreciate any guidance and direction you can offer. Thanks!
EDIT:
Based on comments below there may be a far better way to accomplish my task. So I'm providing some additional background to get direction on other solutions.
This is for a slide show based on partials in a directory. The slide show is controlled with a navigation element which I would like to populate by the h1 tags in the partials. I'm not going to manually enter these things every time a change is made! I want the end user to simply drag and drop partials into a directory (with a certain name convention and h1 tag description for navigation) and let the slide show do everything else.
I could impose a class on the h1 tag "forNavigation" and on the content "sliderContent" and then use jquery to create a post load <ul> but that doesn't seem right. Plus they'll all be part of the same rendered div.
I guess I'm not clear why reading the first 50 characters of a partial, copying whats in the h1 tags, and putting it in a isn't the most elegant solution?
Like I said, above does everything needed except copy and print whats between the first h1 tag... With an xml parser or some regexp it'll be done. I'm just no good with parsing files.
Please let me know other methods to approach this. Right now I still think it's best to parse the partial (with or without rendering) and put what I need where I want it as needed.
Partials are not meant to be "parsed", but to be rendered inside other partials and templates. If you need to grab a part of a partial, you should probably extrat that part as a further partial, and use that inner partial in both the "listed" partial and in the "aggregated" view.

Adding forms in non-form pages in ActiveAdmin

I'm trying to add a simple select box and submit button to a "show" page in ActiveAdmin. Basically, the clients wants a simple way to assign a currently unassigned widget to the item currently being viewed. Not that that really matters.
What I am seeing is that although I can add a form and a select box, if I try to add anything after the select, the select doesn't get displayed. It's not that it is hidden by CSS, but that it just doesn't render.
Here's the relevant code:
column do
panel "Devices without locations" do
devices = Device.without_location
form_tag add_device_admin_location_path do
select_tag(:device_id, options_from_collection_for_select(devices, :id, :name))
submit_tag
end
end
end
The submit tag will be displayed, but the select will not. event if I put "foo" in there, only the "foo" will show up. The only time the select will show up is if there is nothing else in the block.
Update:
Okay, so I've been able to work around this by concatenating the output together. It's not ideal, and I definitely feel dirty, but it works.
I tried using formtastic on this, but it appears to only accepts attributes from the model, this doesn't work: I'm updating the device, not the location.
This works, but if anyone has a more better way of doing this, I'd love to know.
i had the same issue, and moving form into app/views/admin/#{model_name}s/_#{partial_name}.html.erb worked for me fine

RoR- How to use div onclick to render results in a separate area of the page?

I am learning Ruby on Rails, and I am very confused on how the controller-model-view relationship works for my application.
What I have now is a table full of comments (posts) users have made. What I want to do is let users click on a comment to see more information in a separate panel (ie, other database fields that weren't initially shown, for example the user_id of the person who posted the comment).
In my _post.html.erb, I have something like:
<div class="post" id="<%= post.post_id %>" onclick = ?? >
<p>post.text</p></div>
What should go in onclick? I need a way for the onclick to call a helper/controller method which can load more information, and then put that in another div on a page (I've tried variations of using the controller and helper to call javascript which inserts html into the site, but that seems messier than it should be). From what I understand, I should create some kind of partial _postdetails.html.erb file that handles the actual displaying of the html, but I have no idea how to specific where that partial would go in the page.
Any help would be appreciated, thanks!
You can achieve what you want either by using Rails helpers or by writing the AJAX calls yourself.
Personally I manually write all my AJAX calls using jQuery.
You can also use Prototype which ships with Rails.
That being said you can do.
In your JS file :
$("div.some-class").click(function()
{
$.ajax(
{
url:"url/to/controller/action",
type:<GET>/<POST>,
data://If you wish to sent any payload
});
});
In your controller :
def some_action
#some computation
render :update do |page|
page["id_of_div_to_be_refreshed"].replace_html :partial => "some_partial"
end
end

How do you change the displayed order of ActiveScaffold "actions"?

I am using ActiveScaffold in a Ruby on Rails app, and have replaced the default "actions" text in the table (ie. "edit", "delete", "show") with icons using CSS. I have also added a couple of custom actions with action_link.add ("move" and "copy").
For clarity, I would like to have the icons displayed in a different order than they are. Specifically, I would like "edit" to be the first icon displayed.
I seem to be able to change the order of the action_links by the changing the order of definition in the controller. I have also been able to change the order of the default actions by first config.actions.excluding everything, and then adding them with config.actions.add in a specific order.
However, my custom actions always seem to appear before the default actions in the list.
Ideally I would like them to display "edit" "copy" "move" "delete" (ie - built-in, custom, custom, built-in). Can anyone suggest how I might do this?
One idea I had was to re-define "edit" as a custom action (with the default functionality), but I don't know how to go about this either.
Caveat: I don't know ActiveScaffold. This answer is based on me reading its source code.
It looks like the action_links variable is a custom data structure, called ActionLinks. It's defined in ActiveScaffold::DataStructures.
Internally, it has a #set variable, which is not a Set at all, but an Array. ActionLinks has an add, delete, and each methods that serve as gatekeepers of this #set variable.
When displaying the links, ActiveScaffold does this (in _list_actions.rhtml):
<% active_scaffold_config.action_links.each :record do |link| -%>
# Displays the link (code removed for brevity)
<% end -%>
So, short of extending ActiveScaffold::DataStructures::ActionLinks to add a method to sort the values in #set differently, there doesn't seem to be a way to do it, at least not generally.
If I were you, I'd add something called order_by!, where you pass it an array of symbols, with the proper order, and it resorts #set. That way, you can call it after you're done adding your custom actions.

Resources