主干应用POST请求数据库
Backbone app POST request to database
所以我有一个后台使用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。包含其他列的生成器文件
相关文章:
- 如何在执行 Ajax 请求大量数据库更新时实现进度条
- MongoDB MonkAPI根据数据库请求之外可用的查找结果设置变量
- 同步数据库请求节点JS ORM
- 如何在不发出 HTTP 请求的情况下检测 SQL 数据库中的更改
- 将数据从异步请求添加到 WebSQL 数据库时出错
- 使用 ng-repeat同步数据库中请求的数据
- 使用 mySQL、Php 和 JS,我如何请求 dans 使用我在数据库中的数据
- XMLHTTP 请求的 ASP 文件中需要什么才能将数据保存到数据库
- 如何加快具有许多数据库请求的大型网页的速度
- jQuery:使用ajax请求中的所有数据更新数据库
- Node.js并请求2个数据库
- 分析数据库作业没有从http请求中获取所有结果
- 带有get请求的多个数据库查询
- 滚动时对数据库进行双重请求
- 自动通知在数据库的变化:类似于facebook的朋友请求
- Ajax请求、数据库和安全性
- MongoDB / mongab Remove请求在本地工作,但在现场时删除数据库
- 在Node.js中对sqlite数据库进行异步http请求
- TypeError: undefined不是一个函数GET请求/数据库调用
- 主干应用POST请求数据库