主干应用POST请求数据库

Backbone app POST request to database

本文关键字:请求 数据库 POST 应用      更新时间:2023-09-26

所以我有一个后台使用Rails 4的骨干应用程序。有两种模式,文章和出版物。我嵌套了POST请求,因为我需要将从publications帖子返回的信息插入到文章帖子的数据对象中。当我向文章控制器发出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

您需要返回条目值,例如

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

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

原来我必须编辑json。包含其他列的生成器文件