The problem I'm running into deals with rendering a partial. I basically have a link which should get updated after the user puts some text in a text field. The first time it works fine. The user types in the text, the link gets updated and then when the link is clicked the partial is rendered with the correct information. However when the text is changed the link also gets update with the new parameters to pass to the action but when the link is clicked it still renders the old partial and no call to the corresponding action is made. Here is my view code:
label.control-label for="color_servers" Colors
.controls
input.input-xxlarge type="text" name="colors" id="project_colors" placeholder="comma separated colors" onblur="getColors(this)"
a data-toggle="modal" id="color_target" href="#" data-target="#color_modal" (Show colors)
.modal.hide#color_modal
.modal-header
button.close type="button" data-dismiss="modal" aria-hidden="true" ×
h3 Color list
.modal-body
.modal-footer
a.btn href="#" data-dismiss="modal" aria-hidden="true" Close
And this is my partial which I am rendering:
ul
- for color in #colors
li #{color}
I'm displaying the partial with the information in a lightbox type display. Here is my javascript code for the onBlur event of the text field:
function getColors(elem){
if(elem.value.trim()){
$.ajax({
url: "/project/check_colors",
type: "GET",
dataType: "json",
data:{
colors: elem.value
},
success: function(data){
$('#color_target').attr('href','/project/show_colors?colors=' + data.color_list)
console.log(document.getElementById("color_target").href)
console.log(data.input)
console.log(data.color_list)
}
});
}
}
So in the javascript code when I look at the output of the href attribute in the console, it shows the correct link. And finally here is my controller code:
def check_colors
colors = params[:colors].gsub(/\s+/, "").gsub(/,+/,",")
colors = colors.chomp(",")
color_list = Color.expand_colorset(colors).map(&:fullname)
render 'index', :json => {
:servers => color_list,
:input => colors
}
end
def show_colors
colors_string = params[:colors]
#colors = colors_string.split(",")
puts #colors
render partial: 'color_list'
end
The color_list variable is an array of colors which I send back in order to update the link with. I know that the show_colors action gets called called the first time because the #colors variable's value is printed in my terminal however when I update the text in the textfield and I click on the link again the action is not being called even though the link gets updated because nothing is printed in the terminal. It seems as if the partial is being cached, if that is the problem how can I prevent that. Also when I try to render my partial as a full fledged view rather than a partial, the action is called correctly every time and it renders the correct information even after changing the text field contents, but when I do that then my lightbox functionality does not work correctly. Any help will be appreciated, thank you.
hi guys for anyone who is interested I figured it out. It is kind of hacky but it works for me. I added this piece of code in my javascript outside of all functions, and it cleared the cached ajax.
$('body').on('hidden', '.modal', function(){$(this).removeData('modal');});
Hopefully it will save someone time in the future.
Related
I have a home page with a list of software in a table.
With this link I open my softwares pages in modals:
<% = link_to software_path (software), remote: true do %> ... <% end%>
I created a script allowing me to open my modals when I click on td with class .clickable and not open modals when I click on a td with class .no-click.
$(".clickable").click(function(e) {
if (!$(e.target).hasClass('no-click')) {
$('#software-modal').modal('show');
}
});
structure :
_software.html.erb open _software_modal.html.erb
Problem:
When I try to open my modals via the script, it's always the same modal that opens. That is, the modal of software 1 opens when I click on software 2, software 3, ... but no problem when I just use the link.
So I have a problem with id or something like that in my script ...
Can you help me ?
EDIT 1: With the response of VAD I tried to search the id with data-id.
_software.html.erb
<tr class="clickable" data-id="<%= software.id %>">
...
</tr>
<div id='software-content'></div>
_software_modal.html.erb
<div id="software-modal" class="modal fade" role="dialog">
...
</div>
show.js.erb
$('#software-content').html("<%= j render 'software_modal', software: #software %>");
$('#software-modal').modal('show');
this show.js.erb allows me to open my modals via example links:
<%= link_to software.name, software_path(software), remote: true,class:"no-click" %>
but not via a clickable table.
So I added a script in _software.html.erb that makes my table clickable:
$(".clickable[data-id]").click(function(e) {
if (!$(e.target).hasClass('no-click')) {
window.location = '/softwares/' + $(this).attr('data-id');
}
});
I also tried with data-link, ... is there a way to open this window.location in a modal (like the link)? I searched for several hours yesterday but I did not find anything ..
EDIT 2
following the response of #VAD with render layout: false, we can put:
class ApplicationController < ActionController::Base
layout proc{|c| c.request.xhr? ? false : "application" }
end
which allows to have a false render for the js and true render for html. And so to keep my html pages with my starting layout.
Thx #dogenpunk
Render without layout when format is JS (needs drying)
You need to have different modals for every place you want to show the modal from. Like for software1 you need to show the modal with content relative to this particular object. For software2 you'll need to show the same model but with a brand new content relative to software2
Another option (and the better one) is that you may have one single modal for every Software object but by some event (for example actually showing the modal) you'll update the modal content with the data specific for this particular Software object. You may use some controller action for that
Additional
Try to pass software id as data parameters in your clickable elements and then to update your modal content with ajax and like that
<div class = 'clickable' data-id = "#{software.id}">
</div>
$(".clickable").click(function(e) {
if (!$(e.target).hasClass('no-click')) {
$.get('/softwares/' + $(this).attr('data_id'), function(data) {
$('#software-modal body').html(data);
});
$('#software-modal').modal('show');
}
});
Of course I can't give precise code so you'll need to adjust it according to you current setup
Update
The idea is that you have show action in softwares_controller.rb which eventually renders the content for your modals:
def show
// some code you need
render layout: false
end
It will render show.html.erb view. In that view you should keep the html for your modal content. Notice that the action will render that view without layout. You need this because you will soon take this html and will put it into the modal, so you don't need any extra html like the layout.
So, you have an action which render the modal content for every software object by its id.
Now you need to put it into the modal.
You have clickable elements in your markup. You attach correspondent software ids to them as data-id. Then in your jquery code you use these ids to construct a url like this:
'/softwares/' + $(this).attr('data_id')
This url will lead to your show action in softwares_controller.rb. So by clicking on one of clickable elements you take the id, generate the url with it, send request to the url, get the response (the modal content) and then put it into the modal like this:
$.get('/softwares/' + $(this).attr('data_id'), function(data) {
$('#software-modal body').html(data);
});
Then you show the modal with already updated content:
$('#software-modal').modal('show');
There is a pretty bootstrap editor - Bootstrap WYSIWYG, which i want to use in my blog based on RoR 4.0. The problem is that Bootstrap WYSIWYG does not work with anything except DIV tag (as far as i know from a bit searching).
This code works fine:
<div id="editor" contenteditable="true">some editable content</div>
And this one doesnt:
<%= f.text_area :content, id: "editor", contenteditable: "true" %>
<textarea id="editor" contenteditable="true">
So, the question is - how to connect this two things together?
have you tried putting a hidden field, working with the div and when the editor have changes update the hidden field value? Hope this helps
In order to integrate Bootstrap-WYSIWYG in a Ruby on Rails app you should do the following:
(most of the times - you have more than one editor in a rails form, in this example I will show how to do it without errors)
I will use the editor in the admin namespace, therefore I have created a folder editor inside my views: "admin/shared/editor" to keep everything tide and content oriented.
Firstly, for every attribute of a model I would like to use the Bootstrap WYSIWYG editor I will render a partial with the field that has integrated the editor, so you might have smth like this:
/admin/posts/_form.html.haml:
= form_for #post do |f|
= render partial: 'admin/shared/editor/field', locals: { f: f, content: "summary" }
= f.button class: "form_with_editor"
were you pass as a local parameter the form and the attribute of the model you would like to apply the editor (in this case => summary). Note also that you should add a class to the submit button of the form: .form_with_editor, that will be used later for the button click listener.
Now inside admin/shared/editor/_field.html.haml:
.btn-toolbar.btn-editor{"data-role" => "editor-toolbar", "data-target" => "#editor_area_for_#{content}"}
= render partial: 'admin/shared/editor/toolbar'
.wysiwyg{id: "editor_area_for_#{content}", style: "overflow:auto; height:444px;max-height:444px", data: {"for-content" => "hidden_#{content}"}}
= f.hidden_field content.to_sym, id: "hidden_#{content}"
This editor works with a div and not with a textarea, therefore, we will be using a div with class .wysiwyg and a dynamic id which in this case evaluates to: #editor_area_for_summary
The class: .wysiwyg is used in order to select all the divs with this class when we initialize the editor in our doc ready function.
The toolbar partial contains all the markup for the custom toolbar of the editor, you can customize it as you wish and use it in all your fields.
In order to copy the contents of the div to the form input and post them to the server, you have to use a hidden input field:
= f.hidden_field content.to_sym, id: "hidden_#{content}"
Note: it also gets a dynamic id(which evaluates to: "hidden_summary") and a name -> :summary
Now in order to make all these to work together we need some javascript on your doc ready function:
/assets/javascripts/doc_ready.js:
$( document ).ready(function() {
// EDITOR stuff
$('.wysiwyg').wysiwyg();
// Handling the form submission
$('.form_with_editor').click(function(){
$('.wysiwyg').each(function() {
var editor_div_content = $(this).html();
var hidden_input = $(this).attr("data-for-content");
$("#" + hidden_input).val(editor_div_content);
});
});
// Fill the editor divs with content
$('.wysiwyg').each(function() {
var hidden_input = $(this).attr("data-for-content");
var editor_div_content = $("#" + hidden_input).val();
$(this).html(editor_div_content);
});
});
On the first part we apply the function wysiwyg() of the editor in every div that has this class.
On the second part we have an on click handler of the form button we get the html content of the div and we set it as the value of our hidden input field.
And on the last part we do the opposite, we get the value of the hidden field and place it on the editors div when the document is ready, with this one we display content on the editor that is already stored in the database.
I hope this one helps :)
I'm using the following code in a rails partial to place a call to a controller action upon expansion of an accordion element header:
<a> <%= link_to candidate[:title], :controller => :books, :controller=> :confirm_details, :remote => :true, :candidate => candidate %></a>
<div id=<%= candidate[:asin].to_s %>>
</div>
This partial takes a collection of candidates and creates an accordion widget with an anchor as the accordion section header, each such header's href pointing to the books controller action get_details. It also generates a div which has a unique ID for the candidate as it's id attribute.
The get_details action is set up to grab the appropriate div based on that id number and populate its html with data pulled from the controller action.
The problem is that clicking on the anchor tag should both expand the accordion and place a call to the controller, however no http request is being generated. Is Jquery's default behavior to suppress calls from such elements? Should I be using a different tactic? My original thought is that this is the best approach as it lets me use rails' conventional ajax functionality 100%.
Thanks in advance.
What you could do is delegate the click event on those anchors to the accordion element and do whatever you need to place the call to the controller. This handler will be executed along with the default click handler of the accordion:
$( "#accordion" )
.accordion()
.on('click', 'h3 > a', function() {
console.log(this.href);
});
DEMO
I have opened the view template as popup by using the following code:
<%= link_to 'New User', "/users/new", :method => :get, :target => "_blank" %>
controller code:
def new
render :layout => false
end
the new.html.erb is having few textboxes and buttons as Save and Cancel
My problem is how can i close the popup upon clicking Save or Cancel button?
Thank u,
Sudhir C.N.
This a more a javascript question rather than one specific to Rails.
Your Cancel button is easy enough, something like:
<html>
<script>
function CloseWindow() {
ww = window.open(window.location, "_self");
ww.close();
}
</script>
<button type="button" onclick="CloseWindow();">Cancel</button>
</html>
(stolen from http://snippets.dzone.com/posts/show/267)
Your submit button shouldn't change. It will need to POST to your controller as normal. The successful response rendered by the controller should display a "Success" message in the flash and could then close the window automatically after, say, three seconds:
<script>
setTimeout( function() { CloseWindow(); }, 3000);
</script>
This is a pretty simple solution. Maybe consider investigating some popup libraries for either prototype or jquery (depending on the library you're using). Not sure whether modern popup blockers render these D.O.A. though. Your target="_blank" approach should play nice with popup blockers.
Edit: if you're using jquery maybe consider something like the modal dialog: http://jqueryui.com/demos/dialog/#modal-form
You will need to do it with javascript. After the user saves you can respond with another view that sends this:
<script type="text/javascript">
window.close();
</script>
I am trying to write a rails application which lets you go to a certain page, say /person/:id. On this page it shows a set of available resources. I want each resource to have a button next to it, which reserves that resource to that person (by creating a new instance of an Allocation model.) As an extension, I'd like several buttons by each resource, that cancel reservations and do other things. I'd also like to input data alongside some of the buttons, e.g. to allocate some % of a resource.
My problem is I can't work out how to sensibly do this without repeating myself, or having a very hacky controller. How can I do this without matching on the value part of the submit buttons (the text on the buttons), or using any javascript?
Additionally, if you have two forms on a page, how do you set it up so changes on both forms are saved when any submit button is clicked?
im using jQuery, and this is what i did :
<script type="text/javascript">
$(document).ready(function() {
$('#bulk_print').click(function(){
var target = '<%= bulk_print_prepaid_vouchers_path(:format => :pdf) %>';
$('#prepaidvoucher_bulk_print').attr('action', target);
$('#prepaidvoucher_bulk_print').submit();
});
$('#bulk_destroy').click(function(){
var target = '<%= bulk_destroy_prepaid_vouchers_path %>';
$('#prepaidvoucher_bulk_print').attr('action', target);
$('#prepaidvoucher_bulk_print').submit();
});
});
</script>
<% form_tag '#', :method => :post, :id => 'prepaidvoucher_bulk_print' do %>
your form details
<button class="button" type="submit" id="bulk_print">
<%= image_tag("web-app-theme/printer.png", :alt => "Print Selected Vouchers") %> Print Selected Vouchers
</button>
<button class="button" type="submit" id="bulk_destroy">
<%= image_tag("web-app-theme/cross.png", :alt => "Delete Selected Vouchers") %> Delete Selected Vouchers
</button>
<% end %>
The idea is to change the form action on the fly, based on which button is clicked
Make each row in the list a form and put the info about the item in question there. Of course, you'll need to submit and reload the page with each action. The only way around this is to use checkboxes instead of buttons and make it one big form — or to use Javascript.
As for your second question, if you want to have a submit button affect two "forms," you should make them both part of the same form. You can have multiple submit buttons on the form if you need to. Otherwise, you could dynamically generate a third form with Javascript filled with the values from the original form — but that wouldn't work in all cases (e.g., file inputs).