link_to_remote not updating div - ruby-on-rails

I'm using link_to_remote to update a div asynchronously, but it does not work. I have to refresh the page in order to see the change.
I use this to generate the links.
http://ruby.pastebin.com/m1d83be81
"padding-left:30px", :display => "table-row" ) do %>
{ :success => 'entry_' + entry.id.to_s},
:url =>{ :controller => :entries, :action => :increment ,:id => entry.id},
:with => "'amount=' +prompt('Amount')")%>
{ :success => 'entry_' + entry.id.to_s},
:url =>{ :controller => :entries, :action => :decrement ,:id => entry.id},
:with => "'amount=' +prompt('Amount')")%>
{ :success => 'entry_' + entry.id.to_s},
:url =>{ :controller => :entries, :action => :update ,:id => entry.id},
:with => "'amount=' +prompt('Amount')")%>
The corresponding actions all look like this:
def increment
#entry = Entry.find(params[:id])
#entry.amount += params[:amount].to_i
#entry.save!
render :partial=>"entry", :object=>#entry
end

Remove the :success tag in your update clause and test:
:update => 'entry_' + entry.id.to_s

I have found it helpful to specify the div to be updated in an :update in the controller action.

Related

showing error "uninitialized constant Spree::ProductTaxon" while updating spree_products_taxons table in spree

I want to update spree_products_taxons table, but it is showing the error above. What am I am doing wrong?
def import_update
require 'csv'
file = params[:file]
CSV.foreach(file.path, headers: true) do |row|
#prod = Spree::Product.find(row["id"])
#var = Spree::Variant.find_by(product_id: #prod.id)
Spree::Product.where(:id => row["id"]).update_all(:name => row["name"], :meta_description => row["meta_description"], :shipping_category_id => row["shipping_category_id"], :description => row["description"], :meta_keywords => row["meta_keywords"], :tax_category_id => row["tax_category_id"], :available_on => row["available_on"], :deleted_at => row["deleted_at"], :promotionable => row["promotionable"], :meta_title => row["meta_title"], :featured => row["featured"], :supplier_id => row["supplier_id"])
Spree::Variant.find_by(id: #var.id).update(:cost_price => row["cost_price"], :depth => row["depth"], :height => row["height"], :width => row["width"], :weight => row["weight"], :tax_category_id => row["tax_category_id"], :is_master => row["is_master"], :position => row["position"], :cost_currency => row["cost_currency"], :deleted_at => row["deleted_at"], :track_inventory => row["track_inventory"], :tax_category_id => row["tax_category_id"])
Spree::Price.find_by(variant_id: #var.id).update(:amount => row["master_price"], :currency => row["cost_currency"], :deleted_at => row["deleted_at"])
Spree::ProductTaxon.find_by(product_id: #prod.id).update(:taxon_id => row["taxon_id"])
#prop = Spree::ProductProperty.find_by(product_id: #prod.id)
Spree::Property.find_by(id: #prop.property_id).update(:name => row["name"], :presentation => row["presentation"])
Spree::ProductProperty.find_by(product_id: #prod.id).update(value => row["value"])
stock_loc = Spree::StockLocation.find_by(supplier_id: #prod.supplier_id)
Spree::StockItem.where(:variant_id => #variants.id, :stock_location_id => stock_loc.id).update_all(:count_on_hand => row["count_on_hand"], :backorderable => row["backorderable"])
end
redirect_to admin_products_path, notice: "Products Updated."
end
Although the table is called spree_products_taxons (see schema.rb) for some reason the model is Spree::Classification.
You table is spree_products_taxons so it's expecting Spree::ProductsTaxon (mind that its Products NOT Product) model class. Make sure your model should be named as Spree::ProductsTaxon
I can see you are using Spree::ProductTaxon in your code. Please update and try.
Hope it helps.

Rails 2 and ajax - few select lists

I can't figure what i do wrong. I have few select lists:
Regions
Towns
Organizations
How it should works:
User selects region.
List of towns loads.
User selects town.
List of organizations loads.
User chooses organization.
I use ajax for that and i realized only auto-loading for list of towns and i can't do same thing for organization. Second select list DOESN'T POST at all!
Here is my code:
View
%div
%br/
= select 'ajax', :region_id, [['Choose your region...', -1]] + Region.all.map{|region| [region.name, region.id]}.sort
= image_tag('ajax-loader.gif', :id => 'ajax-progress', :style => 'display: none;')
= observe_field :ajax_region_id, :url => { :controller => :organizations, :action => :list_towns, :preselect => (defined?(preselect) && !preselect.nil?) }, :with => 'region_id', :update => 'select_organization_idd', :loading => '$("ajax-progress").show();', :complete => 'if (Ajax.activeRequestCount == 1) $("ajax-progress").hide();'
%br/
= select 'ajax2', :o_id, [], {}, {:id => 'select_organization_idd', :style => 'width: 60em;'}
= image_tag('ajax-loader.gif', :id => 'ajax-progress', :style => 'display: none;')
= observe_field :ajax_region2_id, :url => { :controller => :organizations, :action => :list_region, :preselect => (defined?(preselect) && !preselect.nil?) }, :with => 'o_id', :update => 'select_organization_idd2', :loading => '$("ajax-progress").show();', :complete => 'if (Ajax.activeRequestCount == 1) $("ajax-progress").hide();'
= f.select :organization_id, [], {}, {:id => 'select_organization_idd2', :style => 'width: 60em;'}
Controller
def list_region
render :text => ActionController::Base.helpers.options_for_select((params[:preselect] ? [['Choose organization', -1]] : []) + Organization.map{|org| [ActionController::Base.helpers.truncate(org.name, :length => 155), org.id]})
end
def list_towns
region = Region.find(params[:region_id])
towns = Kladr.find(:all, :conditions => ['code LIKE ?', "#{region.code}%"])
render :text => ActionController::Base.helpers.options_for_select([['Choose town', -1]] + towns.map{ |t| ["#{t.socr}. #{t.name}", t.id] })
end
What do i do wrong? Also i use Rails 2, that why observe_field is not deprecated.
Do you use rails 3? The method observe_field is deprecated.
With rails 3 you must do something like this :
view
%div
%br/
= select 'ajax', :region_id, [['Choose your region...', -1]] + Region.all.map{|region| [region.name, region.id]}.sort, {}, {:id => 'ajax1'}
= image_tag('ajax-loader.gif', :id => 'ajax-progress', :style => 'display: none;')
%br/
= select 'ajax2', :o_id, [], {}, {:id => 'select_organization_idd', :style => 'width: 60em;'}, {}, {:id => 'ajax2'}
= image_tag('ajax-loader.gif', :id => 'ajax-progress', :style => 'display: none;')
= f.select :organization_id, [], {}, {:id => 'select_organization_idd2', :style => 'width: 60em;'}
javascript.js.coffee
$('#ajax1').change = () ->
$.post(
url: '/organizations/list_towns'
data: {value: $('#ajax1').val()}
success: () ->
if (Ajax.activeRequestCount == 1)
$("ajax-progress").hide()
)
$('#ajax2').change = () ->
$.post(
url: '/organizations/list_regions'
data: {value: $('#ajax2').val()}
success: () ->
if (Ajax.activeRequestCount == 1)
$("ajax-progress").hide()
)
The implementation is probably wrong and could be refactored. I didn't test it. But you've got the idea.

Ruby on Rails: Downloading files with multiple dots in filename (uploaded using Carrierwave)

The view:
<%= link_to File.basename(attachment.attachment.url), "/uploads/#{attachment.id}/#{File.basename(attachment.attachment.url)}" %>
The controller:
# ...
def download
path = "#{Rails.root}/uploads/"+ params[:id] + "/"+ params[:basename] +"."+ params[:extension]
send_file path, :x_sendfile=>true
end
# ...
The route:
match "/uploads/:id/:basename.:extension", :controller => "attachments", :action => "download", :conditions => { :method => :get }
The error is get is:
Routing Error
No route matches [GET] "/uploads/38/Screen_shot_2012-02-18_at_2.20.49_PM.png"
match "/uploads/:id/:filename.:extension", :controller => "attachments", :action => "download", :constraints => { :filename => /[^\/]+/ }, :conditions => { :method => :get }
Thanks to forker I was led to this blog: http://coding-journal.com/rails-3-routing-parameters-with-dots/
match "/uploads/:id/:filename", :controller => "attachments", :action => download, :requirements => { :filename => /.*/ }
via this blog post

Ruby 'if' condition in rjs

I want to insert a user in the userlist only if the user object (#row) is not nil. How do I do the insert conditionally in an rjs template?
page.insert_html :bottom, :userlist,
render(:partial => "user", :locals =>
{ :user => #row, :myid => #row.id })
thanks much.
exactly how you would do it in regular ruby
if #row
page.insert_html(:bottom, :userlist, render(:partial => "user", :locals => { :user => #row, :myid => #row.id }))
end
or if you want it on one line
page.insert_html(:bottom, :userlist, render(:partial => "user", :locals => { :user => #row, :myid => #row.id })) if #row
page.insert_html :bottom, :userlist, render(:partial => "user", :locals => { :user => #row, :myid => #row.id }) unless #row.blank?

Rails pass ID from auto_complete field to observe_field

<p>Song Name:<%= text_field :songlists, :song_name, :id => "songlists_" + section.id.to_s + "_song_name" %></p>
<%= auto_complete_field "songlists_" + section.id.to_s + "_song_name", :method => :get,
:with => "'search=' + element.value", :url => formatted_songlists_path(:js) %>
<p>Artist:<%= text_field :songlists, :artist, :id => "songlists_" + section.id.to_s + "_artist" %></p>
<%= observe_field ("songlists_" + section.id.to_s + "_song_name", :update => "songlists_" + section.id.to_s + "_artist", :with => "element.value" , :url => { :controller => :songlists, :action => "get_artist" } ) %>
I have the auto-complete-field working fine, and the observe-field is triggering according to firebug, but I'm unsure how to update the text-field from the observe-field.
I either need to get the ID that the auto-complete finds, or even the text entered so that I can pass it into the :url of the observe-field.
Any ideas?
Can you walkthrough what you want this code to do?
I'm trying to get the response from the auto-complete field, to update another text-field with the results. I have the responses working now in firebug, but I can't seem to get it to update the box with the results.
<p>Song Name:<%= text_field :songlists, :song_name, :id =>
"songlists_" + section.id.to_s + "_song_name" %></p>
<%= auto_complete_field "songlists_" + section.id.to_s +
"_song_name", :method => :get,
:with => "'search=' + element.value",
:url => formatted_songlists_path(:js) %>
<p>Artist:<%= text_field :songlists, :artist %></p>
<%= observe_field "songlists_" +section.id.to_s + "_song_name",
:frequency => 0.5,
:url => { :controller => :songlists,
:action => :get_artist },
:update => "songlists_artist",
:with => "song_name" %>
controller code
def get_artist
#songlist = Songlist.find(:all, :conditions => ['name = ?', params[:song_name]])
if !#songlist.empty?
render :update do |page|
page.replace_html 'songlists_artist', :partial =>
'get_artist', :object => #songlist
end
end
end
In firebug I see this response
try {
Element.update("songlists_artist", "Robbie Williams");
} catch (e) { alert('RJS error:\n\n' + e.toString());
alert('Element.update(\"songlists_artist\", \"Robbie Williams\");');
throw e }
But it is not updating my text-field, any ideas?

Resources