【问题标题】:Trouble with communication between backbone/rails with a REST url带有 REST url 的骨干网/rails 之间的通信问题
【发布时间】:2014-05-13 15:22:41
【问题描述】:

我正在开发 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

感谢您的帮助

【问题讨论】:

    标签: javascript ruby-on-rails rest backbone.js ruby-on-rails-4


    【解决方案1】:

    您必须声明并设置模型的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();
    

    现在你会得到你想要的网址/public_files/ID

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多