Adding forms in non-form pages in ActiveAdmin - ruby-on-rails

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

Related

Can I disable select2 in just one simple_form of my code?

I need to insert a dropdown list in a simple_form collection. I added the line below and it did not work. When I click in the dropdown-box nothing happens and it should show me the collection of itmes (2 email addresses).
= f.input :recipient, :collection => %w[contato#wit.com suporte#wit.com]
When I inspected the code I noticed that everytime I cliked in the dropdown-box something was changing in the code.
From this:
To that:
Then I went to check what was select2 and started to suspect that it migth be the reason why the dropdown-list was not working. Are there any way I can disable Select2 at this part of the code so I can test if this is the reason why my code does not work ? Why is html showing two containers that apparently do the same thing and why the email adresses do not appear ? Find below html which shows select and span containers.

Rails Populating SELECT options from database

I feel like this is basic but can't find details anywhere.
I have a basic application created by generating scaffold.
There is form already built into this that enters data. I have a SELECT (drop down) box in the form. I would like to be able to have the OPTIONS in this select box be pulled from a database. Ideally, the end user would be able to add and edit these options.
I have no idea how to link that SELECT field in my form to where the OPTIONS will be STORED.
Can someone point me in the right direction as far as terminology as what to research? I'm coming up blank. I feel like this must be a common thing to do..
As ByScripts' link includes, here is the script that worked for me (for those where time is not a luxury):
<%= collection_select(:lang_id, 0, Language.all, :id, :name) %>
Where Language is a table with one column called 'name' and an auto-assigned column id; :lang_id is the name of the element and 0 is the default selected index when the page loads.

How to set an initial value for my object in ruby on rails

I am still pretty new to Rails and am working on a basic app and have run into a problem, which I can't seem to get around, sorry if this question has already been answered, but searching didn't yield me any results.
Basically I am creating an app that catalogues all of someones's clothes, Now the main page is the index page of everything they have logged, and each picture is linked to the show page where more information is revealed. I want to use AJAX to bring that show page to the side of the main index page. I am working off of another tutorial to figure this out but have run into a problem. On the initial load of my index page, I need to define a variable for that show page so that it can be rendered using a partial, but I can't use find(params[:id]) since the user hasn't selected anything yet. So my question is how do I set that initial value to either just the first item in the catalogue or not have anything appear, and then when someone does click the link, the sidebar shows the more detailed description of the piece of clothing.
Thanks for any help.
#object = params[:id] ? MyModel.find(params[:id]) : MyModel.first
But I think there's some problem with design of application.
You might have some luck working with the ruby gem 'PJAX'. Here is a screen cast showing you how to get that AJAX sidebar you want. Good luck
It sounds like you can just print the container element as normal, but leave it empty when the page is generated. Optionally, hide it via CSS. Then, when you load its content with AJAX, set it to visible or just populate it as normal.
Alternatively, if you really want to set it to the first item in the catalog (or in any ActiveRecord) you can use .first e.g. Products.first, and use that value to populate its initial contents.

Iterating through website links and clicking them

#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.

How do I tell ActiveScaffold to always show the search form in a list view?

The ActiveScaffold list view has a search form that is loaded via ajax when a user click the search link. I'd prefer to have the form show by default when a user opens a list page.
I've figured out a way to trigger the ajax call when the page loads, but I'm wondering if there's a way to get ActiveScaffold to render the form automatically. Is there a template or a method I can override? I've had a look through the code but there's nothing obvious, at least to me.
Update:
srboisvert's answer inspired me to have a better look.
The trick is to use Template overrides to refactor the following: list.rhtml, _list_header.rhtml, _search.rhtml so that the search form partial renders inline.
There is a way to get it rendered automatically:
active_scaffold :model do |config|
config.list.always_show_search = true
end
I don't currently have an active scaffold project handy but here is how I would figure it out.
I'd use firefox with firebug installed and take a look at what is called when the link is clicked. Then I would go look at that javascript and what it is generating. Then I would search the source for any part of the code or combination that would be fairly unique to the search box ajax. After that it should be easy to cut and past it in without the ajaxyness.
The option
config.list.always_show_search = true
works fine, but only on concrete controller. It throws an exception when used in AS set_default block. Somebody know better solution then to include it in every controller (apart from overriding the template which is handy but complicates version updates)

Resources