简体   繁体   English

骨干应用程序对数据库的POST请求

[英]Backbone app POST request to database

So I have a Backbone app using Rails 4 on the backend. 所以我有一个在后端使用Rails 4的Backbone应用程序。 There are two models, Articles and Publications. 有两种模型,文章和出版物。 I have the POST requests nested since I need info that comes back from the publications post to insert into the data object for the the articles post. 我有嵌套的POST请求,因为我需要从发布发布返回的信息以插入文章发布的数据对象中。 When I make the POST request to the articles controller I send the the following with the data object: url, publication_id and publication_name. 当我向商品控制器发出POST请求时,我将数据对象发送以下内容:URL,publication_id和publication_name。 For some reason when I look at the Rails console at the articles that were created I only see that the url and publication_id were populated but not publication_name. 出于某种原因,当我在所创建的文章中查看Rails控制台时,我只会看到url和publication_id已填充,而未填充publication_name。 Code is pasted here. 代码粘贴在这里。 Any ideas how to get the publication_name to show up? 有什么想法如何使publication_name出现吗?

publications_index.js 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 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 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 article_controller.rb

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

and

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

You need to return the article value, eg 您需要返回商品值,例如

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

Of course, this supposes that you have your controller set to respond_to the json format. 当然,这假设您已将控制器设置为respond_to json格式。 In addition, you'd typically want to return a status in addition to the data. 此外,除了数据之外,您通常还希望返回一个状态。 But the above should return the data you want... 但是上面应该返回您想要的数据...

原来我不得不编辑json.builder文件以包括其他列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM