【问题标题】:Backbone app POST request to database骨干网应用对数据库的 POST 请求
【发布时间】:2014-03-10 03:10:48
【问题描述】:

所以我有一个在后端使用 Rails 4 的 Backbone 应用程序。有两种模型,文章和出版物。我有嵌套的 POST 请求,因为我需要从发布帖子返回的信息插入到文章帖子的数据对象中。当我向文章控制器发出 POST 请求时,我将以下内容与数据对象一起发送:url、publication_id 和 publication_name。出于某种原因,当我在创建的文章中查看 Rails 控制台时,我只看到填充了 url 和 publication_id,但没有填充 publication_name。代码粘贴在这里。任何想法如何让publication_name 出现?

publications_index.js

createFeed: function(e){
    e.preventDefault();
    var feed_url = $('#new_feed_name').val();
    // var that = this;

    this.collection.create(
      { url: feed_url
      },
      { success: function(data){
        var name = data.attributes.name;
        $.post('/articles/force_update', {url: feed_url, publication_id: data.id, publication_name: name}, function(data){
          });
        }
      }
    );

  }

schema.rb

  create_table "articles", force: true do |t|
    t.string   "name"
    t.integer  "publication_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.text     "summary"
    t.string   "url"
    t.datetime "published_at"
    t.string   "guid"
    t.integer  "user_id"
    t.string   "publication"
    t.string   "publication_name"
  end

article.rb

    def self.update_from_feed(feed_url, publication_id, user, publication_name)
    feed = Feedzirra::Feed.fetch_and_parse(feed_url)
    feed.entries.each do |entry|
      unless exists? :guid => entry.id
        create!(
          :name             => entry.title,
          :summary          => entry.summary,
          :url              => feed_url,
          :published_at     => entry.published,
          :guid             => entry.id,
          :publication_id   => publication_id,
          :user_id          => user.id,
          :publication_name => publication_name
        )
      end
    end
  end

articles_controller.rb

  def force_update
    Article.update_from_feed( params[:url], params[:publication_id], current_user, params[:publication_name] )
    render :text => "Success"
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.permit(:name, :publication_id, :created_at, :updated_at, :summary, :url, :published_at, :guid, :publication_name)
    end

【问题讨论】:

    标签: javascript ruby-on-rails post backbone.js model-view-controller


    【解决方案1】:

    需要返回文章值,例如

      def force_update
        Article.update_from_feed( params[:url], params[:publication_id], current_user, params[:publication_name] )
        respond_with(@article)
      end
    

    当然,这假设您将控制器设置为respond_to json 格式。此外,除了数据之外,您通常还希望返回状态。但是上面应该返回你想要的数据......

    【讨论】:

    • 试过了。仍然无法正常工作并且还给了我一个错误:ArgumentError (Nil location provided. Can't build URI.):
    • 这意味着您的set_article 函数没有被调用(或者被无效的id 调用)。
    【解决方案2】:

    原来我必须编辑 json.builder 文件以包含其他列

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-05
      • 2015-01-15
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多