I have a list of images where user can arrange their orders.
When user uploaded an image, I want the list to still be sortable.
I am using a similar upload that was described here: http://kpumuk.info/ruby-on-rails/in-place-file-upload-with-ruby-on-rails/
Please help.
Here are the code for upload in view file:
<% form_for [:admin, #new_image], :html => { :target => 'upload_frame', :multipart => true } do |f| %>
<%= hidden_field_tag :update, 'product_images'%>
<%= f.hidden_field :image_owner_id %>
<%= f.hidden_field :image_owner_type %>
<%= f.file_field :image_file %><br />
or get image from this URL: <%= f.text_field :image_file_url %>
<%= f.hidden_field :image_file_temp %><br />
<%= f.submit "Upload Image" %>
<% end %>
And in controller view:
def create
#image = Image.new(params[:image])
logger.debug "params are #{params.inspect}"
if #image.save
logger.debug "initiating javascript now"
responds_to_parent do
render :update do |page|
logger.debug "javascript test #{sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)}"
page << "show_notification('Image Uploaded');"
page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
page << sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)
end
end
#render :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
else
responds_to_parent do
render :update do |page|
page << "show_notification('Image Upload Error');"
end
end
end
end
Or, to rephrase the question:
Running this:
page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
page << sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)
Will NOT adding sortable list feature.
Please help,
Thank you
I think the problem may well be that the sortable is created when the page first loads. I have run into this issue a couple of times. Adding a new element to the list means you have to call the sortable function again to have the new element included in the sorting.
I am not across prototype/scriptaculous, but there may be a way to have the sortable element listen for events that add elements to the list. jQuery has a set of rebinding events that will respond to new elements added to the DOM, might be something simialr in the other libs.
Found it!
Instead of:
page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
Use
page.replace params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => #image.image_owner, :updated_image => #image}
Related
I am trying to pass the index of something into a partial and am getting a NameError.
Right now, this is my render statement and I am able to access builder just fine.
<%= render 'my_partial', :builder => form_helper %>
But when adding index like below, I get the error.
<%= render 'my_partial', :builder => form_helper, :locals => {:index => index } %>
Any thoughts?
Thanks!
edit:
While trying
<%= render :partial => 'my_partial', :locals => {:builder => form_helper, :index => index } %>
The specific error is "undefined local variable or method `index' for #<#:0x007fc37206ae40>"
Either:
<%= render 'my_partial', :builder => form_helper, :index => index %>
Or:
<%= render :partial => 'my_partial', :locals => {:index => index, :builder => form_helper } %>
I would like to render partials into a view based on conditions. At first I have this view which renders well
<%= simple_form_for [#customer, #reading], :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :customer_id, :editable => false, :value => #customer.id %>
<%= f.input :date_of_reading, :as => :date %>
<%= render 'readings/single_phase', f: f %>
<% end %>
I now want to render partials into the view based on conditions. So i create a helper method in application.rb to do the conditional checking
module ApplicationHelper
def render_readings_conditionally
if #customer.phase_type == 'Single Phase'
render :partial => 'readings/single_phase'
else
render :partial => 'readings/three_phase'
end
end
end
And in my view, I fix the method in there
<%= simple_form_for [#customer, #reading], :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :customer_id, :editable => false, :value => #customer.id %>
<%= f.input :date_of_reading, :as => :date %>
<%= render_readings_conditionally %>
<% end %>
However, this will not work because I have not passed the block argument i had earlier on to my partials.
How can i pass it to my partials?
Just rewrite it this way :
def render_readings_conditionally(form)
if #customer.phase_type == 'Single Phase'
render 'readings/single_phase', f: form
else
render 'readings/three_phase', f: form
end
end
And in your view :
<%= render_readings_conditionally(f) %>
Have a look at view partial locals :: http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables
You can pass any number of variables using locals.
The answer above is passing view locals, although they are being defined shorthand (not explicitly).
You could re-write the code like this, which makes it more clear which variables are being passed to your view partials. You can then access the "form_obj" object in your partial ::
module ApplicationHelper
def render_readings_conditionally
if #customer.phase_type == 'Single Phase'
render :partial => 'readings/single_phase', :locals => { :form_obj => form }
else
render :partial => 'readings/three_phase', :locals => { :form_obj => form }
end
end
I am working in rails 2. I want to make an link_to_remote object and render it on html page. I want to send date via ajax. I am trying this.
render :text => "<font color='"+ color +"'>" +params[:ajax_status] + "</font>" + <%= link_to_remote '| Undo',
:update => 'status_'" +params[:ajax_status]+ ",:url => {:controller => 'requests', :action => 'action_on_punching_request', :id => "+ params[:id] +" :ajax_status => 'Undo'} %>
But unable to proceed. It shows on webpage
Accepted<%= link_to_remote '| Undo', :update => 'status_'Accepted,:url => {:controller => 'requests', :action => 'action_on_punching_request', :id => 20 :ajax_status => 'Undo'} %>
PLease help me out.
You don't need the <%= %> tags, in this case - they're for erb.
Try just:
render :text => "#{ content_tag :font, params[:ajax_status], :color => color }#{ link_to_remote '| Undo', :update => "status_#{ params[:ajax_status] }", :url => {:controller => 'requests', :action => 'action_on_punching_request', :id => params[:id], :ajax_status => 'Undo'} }"
This symptom has been reported in various contexts, most of which I've already searched yet found no clear answer. Here is my context.
In full view index.html.erb:
<div id="add_thingy" style="display:none;">
<%= form_tag (
:action => 'create',
:remote => true,
:update => "thingy_list",
:position => :bottom,
:html => {:id => 'thingy_form'}
) do %>
ipv4_address: <%= text_field "thingy", "ipv4_address" %>
<%= submit_tag 'Add' %>
<% end %>
</div>
In partial view _index.html.erb:
<li><b>
<%= link_to thingy.ipv4_address, "http://"+thingy.ipv4_address+"/index.html", :target => '_blank' %>
<%= link_to 'page1', "http://"+thingy.ipv4_address+"/page1.html", :target => '_blank' %>
<%= link_to 'page2', "http://"+thingy.ipv4_address+"/index.html", :target => '_blank' %>
<%= link_to 'Edit', {:action => "edit", :id => thingy.id} %>
<%= link_to 'Delete', {:action => "delete", :id => thingy.id},
:confirm => "Delete thingy at #thingy.ipv4_address?", :method => :delete %>
</b></li>
In thingy_controller.rb (other things I've tried are shown commented out):
class ThingyController < ApplicationController
layout 'standard'
def index
...
end
...
def create
#thingy = Thingy.new(params[:thingy])
if #thingy.save
# redirect_to :action => 'index'
# $(“#index”).rhtml(“<%= escape_javascript render( #thingy ) %>”);
# render :partial => 'index', :object => #thingy_list
# render :partial => 'index'
render :partial => 'index', :object => #thingy
end
end
...
end
All other aspects of my implementation test clean, including the first commented line above. It's when I try any of the subsequent 4 lines that I get the indicated errors.
I'll use the first commented line for now, though it defeats the main purpose of using Ajax.
Speculation abounds on this question, so I'm looking for information on how it has been fixed in a similar context rather than suggestions on how it might be fixed.
On behalf of myself and the community, thanks for your help!
I have this:
<%= render :partial => "rewards", :collection => #rewards, :as => :reward %>
when I count the items in the collection with <%= #rewards.count %>, it shows 1, and I check ed the db that there is only one reward. However, the partial is rendered twice in my master view, with the second one empty (reward == nil). Any clue on this?
Updates:
Found the reason, before this render statement, there is a user.rewards.build statement for creating a form:
<%= render :partial => "form_reward", :locals => {:user => #user, :reward => #user.rewards.build } %>
<%= render :partial => "rewards", :collection => #rewards, :as => :reward, :locals => {:user => #user } %>
somehow the partial rendered this object also! I guess this is because name pollution. Now how to overcome this problem then?
<%= render :partial => 'rewards', :object => #rewards, :as => :reward %>
You should be able to access it as reward to test try this:
<%= reward.inspect %>
Ohh and of cause if it should be repeatet you should pass it by as a :collection instead of :object like its in my example
I would suggest you to check the query where the #rewards is been populated. It seems like there are two objects in the #reward collection one Reward object and a nil object.
You may use the compact method of the collection to remove the nil object.