Rails simple_forms collections radio button broken down - ruby-on-rails-3

I am attempting to make a voting system for some video. On the vote page I want a radio button for each finalist and next to it an image pulled form youtube the file name and the finalist name. I have a domain for submission and user and made a domain for the voting that is associate to both with the fields submission_id and user_id with submission has_many votes and users has_one vote. Using simple form I create the following:
= simple_form_for #vote do |f|
= f.input :submission_id, :as => :radio, :collection => #finalists, :label_method => :file_name, :value_method => :id, :label => false
= f.submit "Submit Vote 2"
This does work and it puts up a radio button for each and allows the user to select on of the submissions to vote for. However, wanted to put next to the radio button a thumbnail and some text to make it clear what they are voting for. I tried just making a field of HTMl text that of the all the information I wanted:
<img src="youtube/thumbnail" /> <div> Text for label</div>
However, it just prints out the HTML mark up. tried raw(:file_name) but that errored out with bad syntax.
At this point I though I might just need to break down the colleciton and make the loop my self. I tryed the following:
= simple_form_for #vote do |f|
- #finalists.each do |finalist|
= f.input :submission_id, :as => :radio, :value => finalist.id, :input_html => {:name => 'vote[submisison_id]', :id => "vote_submission_id_#{finalist.id}", :value => finalist.id}
= f.submit "Submit Vote"
This results in 5 different radio button collections with their values being yes/no. I of course want one set of radio buttons where one of the submissions gets selected.
I have looked up a few different page on radio_button_tag and collections and I am having a lot of troubles trying to figure out how to tell rails that I want all the radio button to be part of the same set.
Any help would be much appreciated. Thank you.

Related

How to add html link for a simple_form field in rails 3.2?

Here is the quote_task field in simple form.
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => #quote_task.id}, :readonly => true %>
Now we want to add an embeded html link for the #quote_task.id to show the content of the quote_task. When a user click the id, he will be directed to the content of the quote task. Something like:
<%= f.input :quote_task, :label => t('Quote Task'), :input_html => {:value => (link_to #quote_task.id.to_s, quote_task_path(#quote_task.id))}, :readonly => true %>
Is there a way to do this in simple_form? Thanks for help.
your expectation for HTML are way beyond any possible semantic.
http://www.w3schools.com/tags/tag_input.asp
http://www.w3.org/html/wg/drafts/html/master/forms.html#the-input-element
Yes it is possible that when one click on a input, it will show desired content. However without JS this wont be possible
input:
<%= f.input :quote_task, :label => t('Quote Task'), :readonly => true, class="redirecter", :'data-quote-task-path'=>quote_task_path(#quote_task.id) %>
coffee script using jQuery:
#app/assets/javascript/my_input.coffee
jQuery ->
$('input.redirecter').click ->
path = $(this).data('quote-task-path')
document.write(path); # ugly redirect, use Ajax
simple solution, but better would be if you load some content from server to your page with Ajax http://api.jquery.com/jQuery.ajax/
but my opinion is that you shouldn't do this at all. Inputs are for forms, forms are for submitting data. What you should be really using is pure link_to without any input due to HTML semantics. If you want it to look like input that you can style it to look like input, point is don't rape input tag for what it not meant to do.
it's not possible to embed anchors within input fields.
you can use javascript to do whatever magic that field should have.
if you want to find out more about javascript. go to amazon, buy a book, read it.

Display 'show' action in partial within the 'index' action's view - rails

I have a property search page (:controller => 'properties', :action => 'index') that consists of a right sidebar that has a search form which displays the search results below the form. When the user clicks on a property in the right sidebar I want to display the details of that property in the main area of the index page on the left.
Right sidebar is a partial called properties/_property.html.erb:
<%= form_tag properties_path, :method => 'get' do %>
<%= text_field_tag 'location', (params[:location]) %>
<%= select_tag(:max, options_for_select([['No Max', ""], ['$100,000', 100000], ['$200,000', 200000], etc %>
more search fields for baths beds etc
<%= submit_tag "Search", :name => nil %>
<% end %>
<% #properties.each do |property| %>
<%= link_to([property.Address,property.City].join(", "), {:action => 'show', :id => property.id}) %>
<li><strong><%= number_to_currency(property.Price, :precision => 0) %></strong></li>
etc etc
<% end %>
The only way I know how to show the property details is with the 'show' action, but that takes the user to a new page, for example localhost:3000/properties/1865. I've made the 'show' view with the same layout as the 'index' view and have made the right sidebar a partial (properties/_property.html.erb) which appears on both 'show' and 'index' so when the user clicks on a property in the right sidebar and goes to localhost:3000/properties/1865 the property details are displayed correctly in the main area and the right sidebar is on the right.
But because localhost:3000/properties/1865 is a different page than localhost:3000/properties/index the search form in the right sidebar has forgotten it's parameters which means the list of search results in the right sidebar has changed back to the default list of all properties.
How can I display the 'show' action within a partial on the index page so the user's search parameters are remembered by the form in the right sidebar? Or if I have to go to the 'show' page how can I make the right sidebar stay exactly as it is?
Any ideas greatly appreciated, just a suggestion in the right direction would be good, have spent all day trying to figure it out and have got nowhere, thanks
I have made the show view with the same layout as the index view and
have made the right sidebar a partial (properties/_property.html.erb)
which appears on both show and index.
Yes but this will only get you a similar layout for both the pages. You want the list to persist between different requests.
You basically have two options. use session to remember the list which I wont recommend.
Other is you use form :remote => true or ajax and update the page partially.
EDIT:
What version of rails you are using? Do u have jquery loaded in your application?
Follow this SO POST.
You might have to change your show action a bit.
respond_to do |format|
format.js {render :partial => 'property_details' ,:layout => false}
format.html
end
Create a partial for property details and just put body content here.
And link will look like
<%= link_to "link name", {:action => :show, :id => item_id}, :remote => true ,:html => {:class => 'links_product'} %>
Also to update the view you may use(make sure you have rails.js in your page):
$(document).ready(
function(){
$("a.links_product").bind("ajax:success",
function(evt, data, status, xhr){
$("#response").html(data); // in case data is html. (_*.html.erb)
}).bind("ajax:error", function(evt, xhr, status, error){
console.log('server error' + error );
});
});
Have a div with id response or any valid id. Done!

form_helpers Rails 3 - include objects in the loop

Tour has_many :photos, Photo belongs_to :tour.
The fields for a tour are :title, :description.
The fields for a photo are :alt, :image (path), :tour_id
Tour accepts_nested_attributes_for :photos
Tour attr_accessible :photo_attributes
--
In the form for Tours, I want to return the respective Tours photos in the form once saved, so the user can see the photos they have uploaded and add their Alt Tags
This is what the form looks like, but I don't know how to bring back any saved images into the form...
= semantic_form_for ([:admin, #tour]), :html => {:multipart => true} do |f|
...
- unless #tour.new_record?
= semantic_fields_for Photo.new do |f|
= f.file_field :image, :rel => tour_photos_path(#tour)
- else
You must save the tour, to be able add photos.
= f.semantic_fields_for :photos, #tour.photos do |p|
// If there is a photo, somehow display that image in this form loop...
= image_tag ## WHAT COULD I PUT HERE? ##
= f.input :remove_image, :as => :boolean
= p.inputs
I am confused because obviously the form_helper can bring back the saved elements of the form back into the form fields.. but I don't know how I can use one of those saved elements in the image tag...
Your question is a little hard to understand, but what about something like this?
- #tour.photos.each do |photo|
= image_tag photo.path
= f.input :remove_image, :as => :boolean
# etc...

Rails 3 - Unable to create a new post while at the SHOW view of another Post

I have a model named "Post". I want to use a modal form to create a new post while at the SHOW view of another post. Meaning while I am viewing the post named "John" in its show view, I would like to be able to create a new post from right there.
The problem I have is that the ID of the new post remains the same as the post I am viewing, and causes the update action to be fired instead of the create action. Any suggestions on how to handle this?
Build a new post with Post.new and use that in a form_for:
<%= form_for Post.new %>
<%= render "form" %>
<% end %>
Of course this means you'll need to remove the form_for from your form partial if you have it in there, but that's a small sacrifice to make.
However if you really don't want to do that then you will have to pass through a local variable to the form partial to indicate which post you want to display. On the show page you'd have this:
<%= render :partial => "form", :locals => { :post => Post.new } %>
In the new and edit views you'd do this:
<%= render :partial => "form", :locals => { :post => #post } %>
The line is a little bit longer, but that would allow you to keep the form_for tag inside the form partial and not clog up the three other views with it.

How to handle link_to_function in rails3

This is my code
<%= link_to_function ("Add another url", :id=> "add_another_url_link_id") do |page|
page.insert_html :bottom ,:add_another_url, :partial =>'add_another_url', :locals => {:object =>Url.new, :url => section}
end%>
Here i am showing add_another_url partial under the bottom of add_another_ur div.
I have a list. At the end of row i am showing this link. When i click the link that will show form under the row. But when i click the link that is showing the form under the first row only.
I want to show the form for corresponding row. (I don't know how to use 'this' in this place)
Can Anyone help me.
I assume you generate this link for every row in your list. This is a bad idea because it will generate the same element id (add_another_url_link_id) for every row which is not valid html.
You should either generate individual ids:
<%- #items.each do |item| %>
<%= link_to_function "Add another url", :id => "add_url_for_item_#{item.id}" do |page| %>
...
And use the specific id to find the relevant row.