Load table with Kaminari pagination using AJAX.
The pagination itself is using AJAX as well.
In my controller:
def update_user_list
modal = render_to_string('tables/_user_table', :layout => false, :formats=>[:html]).html_safe
data = {
:table => modal
}
respond_to do |format|
format.json { render :json => data }
end
end
In tables/_user_table
# Table part and content notrelated
<%= paginate #users, :params => {:controller => 'product', :action => 'more_users'}, :remote => true, :theme => 'twitter-bootstrap-3' %>
The response JSON of update_user_list looks like:
{
table=" // the table part
// the pagination part shows as follows
<li class="page">3</li>
<li class="page">4
"
}
What I want is to remove ".json" in the url.
If I just render tables/_user_table without AJAX, not in a JSON object, it doesn't have ".json" in the url.
version:
rails: 3.2.17
kaminari: 0.13.0
bootstrap-kaminari-views: 0.0.3
Find the problem. It's actually not included in the question.
$.ajax({
url: '<%= user_product_index_url %>.json',
success: function(data) {
// not important......
});
remove ".json" here solve the problem...
Related
I'm getting an error: No route matches {:action=>"sort", :controller=>"links"}
I'm adapting from a non-nested example and am having trouble figuring out the nested routing.
Right now, my route looks like this:
resources :groups do
resources :navbars do
resources :links do
collection { post :sort }
end
end
post :add_user, on: :member
end
I am rendering a collection of links from navbar>show:
= render partial: 'links/link', collection: #navbar.links
and here's the collection, links>_link.html.haml:
%ul#links{"data-update-url" => sort_group_navbar_links_url}
- #links.each do |faq|
= content_tag_for :li, link do
%span.handle [drag]
= link.method_name
= link.code
= link.button
= link.text
= link_to 'Edit Link', edit_group_navbar_link_path(#group, #navbar, link), class: 'btn btn-mini'
= link_to 'Delete', group_navbar_link_path(#group, #navbar, link), data: { confirm: 'Are you sure?' }, method: :delete, class: 'btn btn-mini'
My links_controller has the sort action:
def sort
params[:link].each_with_index do |id, index|
Link.update_all({display_order: index+1}, {id: id})
end
render nothing: true
end
Because my link collection is being rendered from the navbar>show page, it's not clear to me where my sort action should be located (in links_controller or in navbars_controller).
And my navbars_controller defines #links:
def show
#links = #navbar.links.order("display_order")
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #navbar }
end
end
Also here's the js for good measure (links.js.coffee):
jQuery ->
$('#links').sortable
axis: 'y'
handle: '.handle'
update: ->
$.post($(this).data('update-url'), $(this).sortable('serialize'))
Maybe this last line also needs work to include the route?
I'm using rails 3.2.
I am moving a site from Rails 2 to Rails 3 and need to replace the following deprecated methods, with JQuery:
periodically_call_remote
button_to_remote
In the view, there is a button that when pressed executes a callback every 12 seconds:
#index.html.erb (Rails 2.2.2)
<%= periodically_call_remote(:url => { :action => 'check' }, :frequency => '12', :update => 'log') %>
[...]
<div id="generate_test_btn"><%= button_to_remote "Generate Test Order",
:url => { :action => "check", :should_generate => "YES" },
:update => "log" %>
</div>
[...]
#controller
def check
[...Generate content...]
render :partial => "log" and return
end
My Partial Implementation
Not sure how to glue button_to code to the JQuery AJAX stuff
### index.html.erb (Rails 3.1)
<script>
$(document).ready(function(){
$("#generate_test_button").click(function() {
setInterval(updateTest,12000);
});
});
function updateTest(){
$("#log").load("???");
}
</script>
<%= button_to "Generate Test Order",
{ :controller => :test, :action=> :check, :should_generate => "YES" },
{ :remote => true }
%>
in rails 3 you have unobtrusive javascript
so basically, any link you create with :remote => true would call that controller#action but with a js.erb ending
so in your case the button would call the check action in the test controller
if you create a check.js.erb and place it in the views/test folder and have inside
alert("UJS!");
it should pop up once you press that button
also note that you should have the rails-ujs gem and require it in the application.js manifest
I've implemented the framework outlined in this post: How to use jquery-Tokeninput and Acts-as-taggable-on with some difficulty. This is working insofar as prepopulating with the appropriate theme and ajax search, but when I enter a new tag, it is immediately deleted when the text area loses focus. I'm not sure what I'm doing wrong. Here's some of my relevant code:
User Model (does the tagging):
class User < ActiveRecord::Base
[...]
# tagging
acts_as_tagger
Item Model (accepts a tag):
class Item < ActiveRecord::Base
attr_accessible :title, :tag_list
#tagging functionality
acts_as_taggable_on :tags
Item Controller:
def tags
#tags = ActsAsTaggableOn::Tag.where("tags.name LIKE ?", "%#{params[:q]}%")
respond_to do |format|
format.json { render :json => #tags.collect{|t| {:id => t.name, :name => t.name }}}
end
end
On my form partial:
<%= f.input :tag_list, :label => "Tags", :input_html => { :class => "text_field short", "data-pre" => #item.tags.map(&:attributes).to_json }, :hint => "separate tags by a space" %>
my routes:
get "items/tags" => "items#tags", :as => :tags
resources :items
[almost there!!!]
the js on the form [note: the id of the element is assigned dynamically]:
<script type="text/javascript">
$(function() {
$("#item_tag_list").tokenInput("/art_items/tags", {
prePopulate: $("#item_tag_list").data("pre"),
preventDuplicates: true,
crossDomain: false,
theme: "facebook"
});
});
</script>
If you still want to use Jquery TokenInput and add tags there are different ways to do it.
1.
This is actually from my same question; the newest answer: How to use jquery-Tokeninput and Acts-as-taggable-on
This could go in your controller.
def tags
query = params[:q]
if query[-1,1] == " "
query = query.gsub(" ", "")
Tag.find_or_create_by_name(query)
end
#Do the search in memory for better performance
#tags = ActsAsTaggableOn::Tag.all
#tags = #tags.select { |v| v.name =~ /#{query}/i }
respond_to do |format|
format.json{ render :json => #tags.map(&:attributes) }
end
end
This will create the tag, whenever the space bar is hit.
You could then add this search setting in the jquery script:
noResultsText: 'No result, hit space to create a new tag',
It's a little dirty but it works for me.
2.
Check out this guy's method: https://github.com/vdepizzol/jquery-tokeninput
He made a custom entry ability:
$(function() {
$("#book_author_tokens").tokenInput("/authors.json", {
crossDomain: false,
prePopulate: $("#book_author_tokens").data("pre"),
theme: "facebook",
allowCustomEntry: true
});
});
3.
Not to sure about this one but it may help: Rails : Using jquery tokeninput (railscast #258) to create new entries
4.
This one seems legit as well: https://github.com/loopj/jquery-tokeninput/pull/219
I personally like the first one, seems easiest to get and install.
My goal is to render html from an AJAX call. Changing the dataType to 'html' in my AJAX call seemed to work in that on my console it happily claims that the render was successful:
Rendered admin/articles/show.html.erb within layouts/application (108.1ms)
Completed 200 OK in 185ms (Views: 112.6ms | ActiveRecord: 2.8ms)
but the page does not change.
Here's some code:
View:
<span id='show' class="ui-icon ui-icon-extlink" article_id=<%=article.ID %> onmouseover="this.style.cursor='pointer'"></span>
jquery:
$('#show').live("click",function(evt){
evt.preventDefault();
var article_id=$(this).attr("article_id");
$.ajax({
success : null,
type : 'GET',
url : '/admin/articles/show',
dataType : 'html',
data: {
id: article_id
},
});
});
and my controller:
def show
#article = Article.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => #article }
end
end
routes.rb
match 'articles/show' => 'articles#show', :via => :get
I know it's an odd request to want to render html from AJAX but it's the easiest way that I can think to have a clickable link contained in a span tag.
Using Rails 3.0.1, Ruby 1.9.2 and JQuery
usually ajax request ha js type,if you can change type to js these changes will works
controller
def show
#article = Article.find(params[:id])
respond_to do |format|
format.html
format.js
format.xml { render :xml => #article }
end
end
then in app/views/articles/show.js.erb write javascript code to update page content like
$("#yourDiv1").html("blah blah");
$("#yourDiv2").html("whatever");
and following approach will work for both html and JS
$.ajax({
success : null,
type : 'GET',
url : '/admin/articles/show',
dataType : 'html',
data: {
id: article_id
},
}).done(function( html ) {
$("#yourDIv").html(html);
});
I want to route requests something like this: reports/bloodtypes is routed to controller reports, action bloodtypes, with format = pdf, and the route named as bloodtype_report. The Guides gives an example
match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
When I do this:
match 'reports/bloodtypes' => 'reports#bloodtypes', :defaults => {:format => 'pdf'}, :as => 'bloodtype_report'
or this
match 'reports/bloodtypes' => 'reports#bloodtypes', :format => 'pdf', :as => 'bloodtype_report'
the controller still does not receive the :format => 'pdf' in params, and tries to render the report as HTML. The funny thing is that the route is shown by Rake as
bloodtype_report : /reports/bloodtypes(.:format) : {:format=>"pdf", :controller=>"reports", :action=>"bloodtypes"}
whether I use the first form (with :default) or second (just setting the format to pdf). It seems the route is correct, so why is the format parameter not being passed to the controller?
have you tried adding this to your controller:
respond_to do |format|
format.html
format.pdf { render :pdf => "show" }
end