骨干网/rails之间使用REST url通信有问题

Trouble with communication between backbone/rails with a REST url

本文关键字:REST url 通信 有问题 rails 之间 骨干网      更新时间:2023-09-26

我正在开发Rails x Backbone应用程序。我有一个名为Public_files的rails控制器,它允许我在公共文件夹上的文件上工作。

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

现在我想用Backbone在客户端处理文件。所以我写了一个Backbone模型。在我定义urlRoot的地方,我的模型将与rails的REST url通信。

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

我只是想试试这是否有效。但我真的不知道如何在JS中实例化一个PublicFile对象,它的ID将是我到文件的路径。之后我可以创建一个my_file.fetch()它会在/public_files/ID

上做一个GET操作

谢谢你的帮助

您必须声明并设置模型的idAttribute:

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

然后在模型实例中,设置id并获取模型:

yourModel.set('yourId', theIdValue);
yourModel.fetch();
现在你将得到你想要的url /public_files/ID