javascript - Backbone app POST request to database -
so have backbone app using rails 4 on backend. there 2 models, articles , publications. have post requests nested since need info comes publications post insert data object the articles post. when make post request articles controller send the following data object: url, publication_id , publication_name. reason when @ rails console @ articles created see url , publication_id populated not publication_name. code pasted here. ideas how publication_name show up?
publications_index.js
createfeed: function(e){ e.preventdefault(); var feed_url = $('#new_feed_name').val(); // var = 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 |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 |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
and
private # use callbacks share common setup or constraints between actions. def set_article @article = article.find(params[:id]) end # never trust parameters scary internet, allow 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 return article value, e.g.
def force_update article.update_from_feed( params[:url], params[:publication_id], current_user, params[:publication_name] ) respond_with(@article) end
of course, supposes have controller set respond_to
json format. in addition, you'd typically want return status in addition data. above should return data want...
Comments
Post a Comment