javascript - Updated - Gon Plugin not refreshing - Rails 4 Partial is not refreshing when using a Find -
this first post here, i've been struggling think (thought) easy. lol
i using tutorial here integrate dropzonejs rails app: http://geekhmer.github.io/blog/2015/02/10/ruby-on-rails-uploads-multiple-files-with-dropzonejs-and-paperclip-gem/
it works is, , if when reference controller (cars) works is. trouble want display images match car id of current car. have car.id set match car_id field of images table, upload dropzonejs field adds db , works fine, when try filter partial @ can't refresh when adding new images.
i have 2 controllers i'm testing on. images:
class imagescontroller < applicationcontroller def index @images = image.all end def create @image = image.new(image_params) if @image.save render json: { message: "success", fileid: @image.id }, :status => 200 else render json: { error: @image.errors.full_messages.join(',')}, :status => 400 end end private def image_params params.require(:image).permit(:avatar, :car_id) end end the car controller is:
class carscontroller < applicationcontroller before_action :set_car, only: [:show, :edit, :update, :destroy] before_filter :load_images # /cars # /cars.json def index @cars = car.all end # /cars/1 # /cars/1.json def show end # /cars/new def new @car = car.new end # /cars/1/edit def edit end # post /cars # post /cars.json def create @car = car.new(car_params) respond_to |format| if @car.save format.html { redirect_to @car, notice: 'car created.' } format.json { render :show, status: :created, location: @car } else format.html { render :new } format.json { render json: @car.errors, status: :unprocessable_entity } end end end # patch/put /cars/1 # patch/put /cars/1.json def update respond_to |format| if @car.update(car_params) format.html { redirect_to @car, notice: 'car updated.' } format.json { render :show, status: :ok, location: @car } else format.html { render :edit } format.json { render json: @car.errors, status: :unprocessable_entity } end end end # delete /cars/1 # delete /cars/1.json def destroy @car.destroy respond_to |format| format.html { redirect_to cars_url, notice: 'car destroyed.' } format.json { head :no_content } end end private # use callbacks share common setup or constraints between actions. def set_car @car = car.find(params[:id]) end def load_images @images = image.all @image = image.new end # never trust parameters scary internet, allow white list through. def car_params params.require(:car).permit(:name, :model, :carpic) end end here show view i'm using cars/show.html.erb
<p id="notice"><%= notice %></p> <p> <strong>name:</strong> <%= @car.name %> </p> <p> <strong>model:</strong> <%= @car.model %> </p> <p> <strong>carpic:</strong> <%= image_tag @car.carpic(:medium)%> </p> <h1>my images</h1> <%= form_for(image.new, html: { multipart: true, class: "dropzone"}) |f| %> <%= f.hidden_field :car_id, :value => @car.id %> <div class="fallback"> <%= f.file_field :avatar %><br> <%= f.submit "upload avatar" %> </div> <% end %> <div class="index"> <%= render partial: 'images/index' %> </div> <%= link_to 'edit', edit_car_path(@car) %> | <%= link_to 'back', cars_path %> here partial images/_index.html.erb <% @images.each |image| %> <div class="img-thumbnail"><%= image_tag image.avatar.url(:thumb), alt: image.avatar.url(:thumb) %></div> <% end %> here app/assets/javascripts/image.js
$(document).ready(function(){ // disable auto discover dropzone.autodiscover = false; var dropzone = new dropzone (".dropzone", { maxfilesize: 256, // set maximum file size 256 mb paramname: "image[avatar]", // rails expects file upload model[field_name] addremovelinks: false // don't show remove links on dropzone itself. }); dropzone.on("success", function(file) { this.removefile(file); $.getscript("/images"); }) }); and here views/images/index.js.erb
$(".index").html("<%= escape_javascript(render('index')) %>") again works if don't try filter results of @images in partial display image.car_id. displays them doesn't refresh when dropzonejs uploads file when doesn't have filter on.
i've tried in partial _index
<% image.where(car_id: @car.id).find_each |image| %> <div class="img-thumbnail"> <%= image_tag image.avatar.url(:thumb), alt: image.avatar.url(:thumb) %> </div> <% end %> again, works doesn't refresh when it's not being filtered. have played around in controller no success either.
also, have relationships setup right, images belongs cars, cars has many images.
any appreciated! been trying week!
edited add below
so i've been able work gon gem, have new problem, here code below:
the show controller, i've added:
def show gon.watch.carid = @car.id end to top of includes in application.html.erb i've added:
<%= include_gon(:watch => true, :init => true) %> my image.js file looks this:
$(document).ready(function(){ // disable auto discover dropzone.autodiscover = false;
var dropzone = new dropzone (".dropzone", { maxfilesize: 256, // set maximum file size 256 mb paramname: "image[avatar]", // rails expects file upload model[field_name] addremovelinks: false // don't show remove links on dropzone itself. }); dropzone.on("success", function(file) { this.removefile(file); $.getscript(gon.watch.carid); }) }); so here's new problem, if i'm adding images car.id"1" go car index , click show link car.id"2" when add new image via drop zone cars in gallery car.id"2" switches car.id"1" until click refresh on page, resets variable in gon.watch.carid.
i assumed, apparently incorrectly calling show method display second car should reset gon variable? if not gon watch , init call in application.html.erb file thought should it.
has worked gon or have idea how solve this?
thanks! d
i able solve moving gon include statement head body of application.html.erb file, fine now!
hopefully helps else!
d
Comments
Post a Comment