Rails default select value - ruby-on-rails

This is my first post, and I apologiese for repeating a previous question. But I'm new to Rails and need help from someone more experienced than I. I am trying to get a select box to select the database record.
My form reads:
I have tried adding
,params[:source]
And a few other methods which don't include the option for a select box. Could someone guide me as to what is missing to select my current record. Any info can be provided on request. I am using the current version of Rails on Mac OS. Thank you

As per the description mentioned in the post:
I am trying to get a select box to select the database record.
But you are passing statement values in options for select
<%= form.select(:source, options_for_select([['Thumbstack', 'Thumbstack'], ['Teacher', 'Teacher'], ['Google', 'Google'], ['Other', 'Other']])) %>
You need to change above one to the below mentioned:
<%= form.select(:source, options_for_select(ModelName.all.map {|h| [h.id, h.column_name]})) %>
Hope it helps!!

Related

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.

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

Rails 3.0 . How to pass the select box value through link_to

I knew that Rails3 no longer supporting link_to_remote.... instead of that we can modify
the link_to method by using different syntax as follows :
link_to "Send",{:action =>"send_mail",:question_id =>question.id},:remote => true,:class =>"button small"
My problem is, in my view i keep the select box which contains the list of user's name near by send link button (which user the above syntax)... i want to pass the selection box value to link_to when user click the send button
Here is my View code :
"send_mail",:question_id =>question.id,:user_value
=>encodeURIComponent($F('user_id'))},:remote => true,:class =>"button small" %>
I don't know how to achieve this ....can any one please suggest me on this.
edit: ok now is see your last comment... never mind
What I got from the question, you are looking for something like this:
http://marcgrabanski.com/articles/jquery-select-list-values
It's not really a Rails problem since you can't change the Ruby code from within the rendered HTML (at least that would be very risky if it's possible). The code from above can be easily changed to your needs so that the user gets redirected to the URL that matches the button value.

Rendering field data as a link in Ruby on Rails

Ok, I think this is probably an easy question but for the life of my I can't figure it out. I have created a table called ugtags and in that table I have two columns (beyond the basics), 'name' and 'link'.
I am trying to allow a user to add a link to a page. Ideally they would enter the link title (name) and the url (link) and in the view it would display the title as a link to the url that was entered in the link column.
I there a way to do it by simply affecting the <%= link_to h(ugtag.name) %> code?
You should just be able to do:
<%= link_to h(ugtag.name), ugtag.link %>
See the documentation for all of the relevant options.

How to generate a select box by using options_from_collection_for_select

I am really confused with select boxes in ROR. How can I generate a working select box by using options_from_collection_for_select. This will only generate the option tags and not the select tag? Can anyone please provide me an example on how to use this.
I am having a hard time understanding select boxes, I don't know why...
Thanks
<% options = options_from_collection_for_select(#awesome_options, 'id', 'name') %>
<%= f.select :awesome_column, options %>
If you could give a little more insight into the error you are getting, we might be able to provide you with a better answer, but I believe the above is the basic gist of options_from_collection.

Resources