rails-api

Can I delete all migration files and start from scratch?

耗尽温柔 提交于 2019-12-20 04:32:12
问题 I have a Rails-API app that I'm going to re-deploy after a long time. The app is non-production, but I am ready to deploy the production version. I want to basically delete all the migration files and start from scratch using the schema, is there any problem with this approach? Assuming I can do this, what do I need to alter the schema.rb to? Is this a common practice? Seems reasonable to me. # what do I change the version param value to? ActiveRecord::Schema.define(version: 20171129023538)

Rails 5.1 API - incorrect url_for with a nested resource

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 03:04:20
问题 Given this route structure: routes: namespace :api do resources :templates do resources :template_items end end I scaffolded the template_items controller (but cannot recall exactly what I specified) but it gave me this create action: # POST /template_items def create @template_item = TemplateItem.new(template_item_params) if @template_item.save render json: @template_item, status: :created, location: @template_item else render json: @template_item.errors, status: :unprocessable_entity end

Nested routes and CRUD operations with additional values for assciation in Rails

偶尔善良 提交于 2019-12-13 01:19:34
问题 I created one has_many through relationship in rails api. I also used nested routes. My models like below; class Author < ActiveRecord::Base has_many :comments has_many :posts, :through => :comments end class Post < ActiveRecord::Base has_many :comments has_many :authors, :through => :comments end class Comment < ActiveRecord::Base belongs_to :author belongs_to :post end my routes like below; Rails.application.routes.draw do namespace :api, defaults: { :format => 'json' } do resources :posts

Factory Girl uninitialized constant Model Name with rails-api

允我心安 提交于 2019-12-13 00:29:29
问题 I'm trying to make a factory for my User model, but when I run rspec, I get Failure/Error: user = build(:user, email: "invalid@email.com") NameError: uninitialized constant User When I hop into rails console, I can do User.all and it will return the users I have and FactoryGirl.build(:user, email: "invalid@email.com") works just fine, so I'm not sure where the problem is. I've created the project with rails-api, using rails 4.2 ruby 2.2 # spec/spec_helper.rb # i have to set spec/factories as

REST API versioning - why aren't models versioned

自闭症网瘾萝莉.ら 提交于 2019-12-12 10:34:51
问题 I've been reading up on all the approaches to version REST APIs. In almost all implementations, controllers and views are versioned, however models are not. To give the rails example, controllers are organized as: # app/controllers/api/v1/events_controller.rb class Api::V1::EventsController < Api::ApiController end Corresponding views as put in at different versioned directories as well. Why don't we version models? Is it because we expect our model (underlying database schema) to not change

Getting information from one Rails server to Another

冷暖自知 提交于 2019-12-12 06:03:12
问题 I'll try to keep this as brief as possible and open the question to discussion. I have a Rails app which is a static codebase and runs on 9 different servers all the same db schema but of course with different values. I wrote some SQL to query some dollar totals and will be putting this into either a rake task or a sidekiq worker and have it fire once a week to generate the data. Initially I was thinking of just throwing the resulting data from each server into a mailer and mailing it to

Managing multiple GET parameters in Rails 5 API

[亡魂溺海] 提交于 2019-12-12 01:27:29
问题 I'm using Rails 5 API to build an app for retrieving data. I want to use multiple GET parameters to extract data. Sample GET Request Format /users?company=Samsung&position=Engineer Controller Code for single parameter:- def index client = User.where("company = ?", params[:company]) render json: client end Controller Code for multiple parameters:- def index client = User.where("company = ?", params[:company]).where("position = ?", params[:position]) render json: client end Likewise there are

Rails API/Pundit: Strong parameters with ActiveModelSerializers

为君一笑 提交于 2019-12-11 14:55:33
问题 This section of Pundit section says that we could control which attributes are authorized to be updated. But it fails in case of the use of active_model_seriallizers gem: def post_params # originally geneated by scaffold #params.require(:post).permit(:title, :body, :user_id) #To deserialize with active_model_serializers ActiveModelSerializers::Deserialization.jsonapi_parse!( params, only: [:title, :body, :user] ) end If I modify the PostsController update action as Pundit suggested: def

Ruby API - Accept parameters and execute script

坚强是说给别人听的谎言 提交于 2019-12-11 13:09:34
问题 I have created a rails project that has some code that I would like to execute as an API. I am using the rails-api gem. The file is located in app/controllers/api/stats.rb. I would like to be able to execute that script and return json output by visiting a link such as this - http://sampleapi.com/stats/?location=USA?state=Florida. How should I configure my project so that when I visit that link it runs my code? 回答1: the file should be called stats_controller.rb app/controllers/api/stats

How can I use resque-web for rails api-only app?

空扰寡人 提交于 2019-12-11 06:55:38
问题 I believe resque-web requires ActionView to work correctly. I have a rails api-only app. I plan to run the resque workers on a separate machine using a shared redis server. How can I make resque-web work and still maintaining the app as lean as possible? 来源: https://stackoverflow.com/questions/52233406/how-can-i-use-resque-web-for-rails-api-only-app