Question

I'm working on a Rails x Backbone app. I got a rails controller called Public_files that allow me to work on the files on the public folder.

class PublicFilesController < ApplicationController
  skip_before_action :verify_authenticity_token
  before_action :set_public_file, only: [:show, :edit, :update, :destroy]

  # GET /public_files/id
  def show
    path = params[:id]
    if File.exists?( Rails.public_path.join( "#{path}.html" ) )
      @content = File.read( Rails.public_path.join( "#{path}.html" ) )
      render file: "public/#{path}", formats: [:html]
    else
      render file: 'public/404', status: 404, formats: [:html]
    end
  end

  # GET /public_files/id/edit
  def edit
    path = params[:id]
    if File.exists?( Rails.public_path.join( "#{path}.html" ) )
      @content = File.read( Rails.public_path.join( "#{path}.html" ) )
      render file: "public/#{path}", formats: [:html]
    else
      render file: 'public/404', status: 404, formats: [:html]
    end
  end

  # POST /public_files
  def create    
    path = params[:public_file]
    content = params[:content]
    if File.exists?( Rails.public_path.join( "#{path}.html" ) )
      puts "The file you want to create already exist"
      render file: 'public/404', status: 404, formats: [:html]
    else
      File.write(Rails.public_path.join( "#{path}.html" ), "#{content}")
      render file: "public/#{path}", formats: [:html]
    end
  end

  # PATCH/PUT /public_files/id
  def update
    path = params[:path]
    content = params[:content]
    if File.exists?( Rails.public_path.join( "#{path}.html" ) )
      File.write(Rails.public_path.join( "#{path}.html" ), "#{content}")
      render file: "public/#{path}", formats: [:html]
    else
      puts "The file you want to update doesn't exist"
      render file: 'public/404', status: 404, formats: [:html]
    end
  end

  # DELETE /public_files/id
  # DELETE /public_files/id.json
  def destroy
    path = params[:id]
    if File.exists?( Rails.public_path.join( "#{path}.html" ) )
      File.delete( Rails.public_path.join( "#{path}.html" ) )
    else
      puts "The file you want to delete doesn't exist"
    end
    respond_to do |format|
      format.html { redirect_to public_files_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_public_file
      # @public_file = PublicFile.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def public_file_params
      params[:public_file]
    end
end

Now i want to handle the files on the client side with Backbone. So i wrote a Backbone model. Where i define the urlRoot like that my model will communicate with the REST url of rails.

class MyApp.Models.PulicFile extends Backbone.Model
  urlRoot: '/public_files'

And i just want to try if that's working. But i don't really know how to instantiate a PublicFile object in JS with an ID that will be my path to the file. So that after i can make a my_file.fetch() and it will make a GET on /public_files/ID

Thank you for your help

Was it helpful?

Solution

You have to declare and set the idAttribute of your model:

class MyApp.Models.PulicFile extends Backbone.Model
  urlRoot: '/public_files'
  idAttribute: 'yourId' // or the default value 'id'

and then in your model instance, set the id and fetch the model:

yourModel.set('yourId', theIdValue);
yourModel.fetch();

Now you will get the url that you want /public_files/ID

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top