How to handle link_to_function in rails3 - ruby-on-rails-3

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.

Related

Rails 5 - use collection_radio_buttons to open a partial with nested models?

(my first SO question, so please be kind!)
Each Schedule has only one System;
Each System has many Applications;
Each Application has many Users and Documents.
What I want to do to create a Schedule entry:
Generate a form that first shows multiple System names that can be clicked. When a System is clicked, it opens a partial that lists Applications associated with that System. Then, when clicking particular Applications, yet another partial opens that contains Users and Documents associated with that particular Application.
When editing this entry later, I want to be able to see everything I had entered before, with the correct System, Application(s), User(s), and Document(s) already pre-selected.
My question here is how to make a form element for choosing a System that will open another partial -- and, later, will be pre-selected when I view and/or edit the entry.
What kinda works right now is a <%= link_to %>, styled with Bootstrap, which opens its associated Applications partial when it's clicked. However, I'm not able to save the System from it, and I can't figure out whether it can display as already-selected later, such as in an Edit form.
We're trying to use radio buttons instead (because you can't pre-select a link_to, right?), so I've been throwing pasta at the wall with f.collection_radio_buttons, or iterating over f.radio_button, or a tag of <input type="radio">. However, I can't figure out how to make a radio button open a partial.
Since first posting this question, I've narrowed down to using f.radio_button within a loop. It shows as correctly "checked" when viewed while Editing the entry later, but it still doesn't open the partial.
Here's a snippet from /_schedule_form.html.erb:
<%= form_for #schedule do |f| %>
<% System.all.each do |a| %>
<!-- This link_to makes the Applications partial appear, but that's all it does -->
<%= link_to a.system_nm, system_applications_path(:system_id => a.id,
:schedule_id => params['id']), :title => 'Click to choose system',
:class -> 'btn btn-default btn-flat active', data: {:remote => true,
'data-target' => '#applicationList'} %>
</a> <!-- closes the link_to tag, I believe -->
<% end %>
<div id="applicationList">
<!-- the Applications partial renders here -->
</div>
<% end %>
Here's the system_applications.js.erb file that opens the _system_applications.html.erb partial:
$("#applicationList").html("<%= escape_javascript(render
'system_applications', locals: {applications: #applications,
schedule_applications_array: #schedule_applications_array})%>");
Here's an update with possible clue:
I'm now using this embedded Ruby code:
<% System.all.each do |rt| %>
<label>
<%= f.radio_button :system_id, rt.id, data:{:remote => true, 'data-target' =>
'#applicationList'}, href: system_applications_path(:system_id => rt.id,
:schedule_id => params['id']), class: 'remote-input', onclick:
"#applicationsList" %>
</label>
<% end %>
And, when I click on that radio button, I'm getting a JS error in the browser console Uncaught SyntaxError: Invalid or Unexpected Token which points to the rendered HTML, and specifically the > at the end of the line:
<input data-remote="true" data-data-target="#applicationList" href="/schedules/system_applications/24?schedule_id=122" class="remote-input" onclick="#applicationList" type="radio" value="24" checked="checked" name="schedule[system_id]" id="schedule_system_id_24" />
Just to make it more complicated:
When creating a NEW entry, and when I hover over one of the link_to buttons, I get a clean-looking path like /schedules/system_applications/24, and that's what gets sent to the server when clicked (which then reads the params as {"system_id"=>"24"}. But hovering over the f.radio_button labels shows no path, and clicking it sends "/schedules/new?schedule%5Bsystem_id%5D=24" with the params {"schedule"=>{"system_id"=>"24"}}. This triggers a 500 (Internal Server Error) (which makes sense, because there's no path for that URL). Do the params need to be the same? If so, how am I screwing it up?
Also, clicking the radio button sends the request to SchedulesController#new, while clicking the link_to sends it to SchedulesController#system_applications. I don't understand how I'm telling the radio button to send it to #new.
Where I'm stuck now is, the f.radio_button renders as "checked", which is correct; but it doesn't open the partial. The link_to version opens the partial, but I don't think it can render as "checked" later on.
Let me know if I'm asking clearly enough, too.
I think we made it work. One key change was to use url instead of href to use the system_applications_path to the controller, as shown here:
<% #systems.each do |a|
<label class="btn btn-sm btn-default">
<%= f.radio_button :system_id, a.id, :data => {url:system_applications_path(:system_id
=> a.id, :schedule_id => params['id']), 'data-target' => '#applicationList'+a.id.to_s,
:remote => true} %>
<%= a.system_nm %>
</label>
<% end %>
Now, when I click the radio button, it opens the correct partial, and it shows as selected later when I go to edit the entry. The Bootstrap styling (btn btn-sm btn-default) helps to show how much clickable area there is, but it's not required for basic functionality.
I suppose some of that [in]famous Rails Magic has radio_buttons treating href: differently than url. But it's "magic" only because I don't understand it (yet -- growth mindset!). What's the explanation behind it?

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!

Rails simple_forms collections radio button broken down

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.

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.