所以我在后端使用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
答案 0 :(得分:0)
您需要返回文章值,例如
def force_update
Article.update_from_feed( params[:url], params[:publication_id], current_user, params[:publication_name] )
respond_with(@article)
end
当然,这假设您将控制器设置为respond_to
json格式。此外,除了数据之外,您通常还希望返回状态。但上面应该返回你想要的数据......
答案 1 :(得分:0)
原来我必须编辑json.builder文件以包含其他列