Rails api with paperclip - ruby-on-rails

I have rails api with simple paperclip model:
def create
#photo = Photo.new(photo_params)
if #photo.save
render json: #photo, status: :created, location: #photo
else
render json: #photo.errors, status: :unprocessable_entity
end
end
private
def photo_params
params.require(:photo).permit(:image, :title)
end
My frontend framework send get like this
{"title"=>"simpletitletext", "photo"=>{"image"=>......}}
But it wrong, because rails waits following
{"photo"=>{"title"=>"simpletitle", "image"=>#...}}
I had been trying for different ways to fix angular for many hours, before wrote . May be it will be able to fix in rails

If your server has an incoming request that looks like this:
{"title"=>"simpletitletext", "photo"=>{"image"=>......}}
you can make it look like this:
{"photo"=>{"title"=>"simpletitle", "image"=>#...}}
The only difference between the two is that in the first, the title key is outside of the photo nested hash. but you want it to be inside.
So in your Rails controller,. you could write:
def photo_params
hash = params[:photo]
hash.merge(title: params[:title])
end

Related

Rails: Find or initialize and then merge in params

I use find_or_initialize_by in my controller and then would like to apply the additional params to the record, whether new or existing. Is this possible to do in a Railsy way or do I have to loop through the parameters?
def create
re_evaluation = #client.re_evaluations.find_or_initialize_by(program_id: re_evaluation_params[:program_id], check_in_id: re_evaluation_params[:check_in_id])
# apply remaining re_evaluation_params here
if re_evaluation.save
render json: re_evaluation, status: :created
else
render json: re_evaluation.errors.full_messages, status: :unprocessable_entity
end
end
You should be able to assign the parameters as you'd do normally.
re_evaluation.assign_attributes(re_evaluation_params)
if re_evaluation.save
...

There is no output of information from the database on the client

For some reason, the client does not display information from the database. I do not know what the problem is anymore.I use Rails Api, Angular, and the Sqlite3 database.The application was deployed on the Nginx web server using puma and capistrano.
Maybe I missed something posts_controller.rb:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :update, :destroy]
def index
#posts = Post.all
render json: #posts
end
def show
render json: #post
end
def create
#post = Post.new(post_params)
if #post.save
render json: #post, status: :created, location: #post
else
render json: #post.erros, status: :unprocessable_entity
end
end
def update
if #post.update(post_params)
render json: #post
else
render json: #post.errors, status: :unprocessable_entity
end
end
def destroy
#post.destroy
end
private
def set_post
#post = Post.find(params[:id])
end
def post_params
params.require(:post).permit!
end
end
I'd suggest checking your Angular app, maybe something is crashing on the JS side. Maybe making sure Rails is returning data by running a REST request using a chrome REST extension or something.
Make sure you're running the same Rails environment, and the database (if you're on development somewhere, and production somewhere else, databases will differ.
You can always go to rails console and make sure Post.all returns content. Your controller seems correct.

Rails render status: :not found missing template error

While developing an app for a course,I hit upon a stumbling block:
The error screen
And here is my Stocks Controller error, where the error appears:
class StocksController < ApplicationController
def search
if params[:stock]
#stock = Stock.find_by_ticker(params[:stock])
#stock ||= Stock.new_from_lookup(params[:stock])
end
if #stock
render json: #stock
#render partial: 'lookup'
else
render status: :not_found ,nothing: true
end
end
end
On the course, they have the same code as I do,but for them, it works.The only difference I am aware of is that they are working on Rails 4(Nitrous),and that I am working on Rails 5(Mac OS X/Atom IDE/GitLab repository).Please help me if you can!Thank you in advance!
:nothing option is deprecated and will be removed in Rails 5.1. Use head method to respond with empty response body.
Try this:
render body: nil, status: :not_found
or:
head :not_found
Please don't post errors as images, copy-past the text
The problem here is that you are not rendering json in else clause and hence Rails for looking for a HTML view which is not present. To fix this, update the code something as below:
class StocksController < ApplicationController
def search
if params[:stock]
#stock = Stock.find_by_ticker(params[:stock])
#stock ||= Stock.new_from_lookup(params[:stock])
end
if #stock
render json: #stock
#render partial: 'lookup'
else
render :json => {:error => "not-found"}.to_json, :status => 404
end
end
end

Template is missing with JBuilder

I want to skip JBuilder for certain controllers and just use the as_json defined on the model. I find I often want to code the JSON uniformly there on the model, but in some cases I need Jbuilder.
Model
def as_json(options={})
super(only: :uuid)
end
Controller
render json: {success: #model}
I continue to get an error exclaiming template is missing.
EDIT: This is my entire render action right now, this is using devise.
def device_session
if signed_in?
render json: {user: current_user}
else
render json: {fail: 'Could not authenticate device'}
end
end

show paperclip ( images) which stores in another app, current app already connect with the external database,Ruby on rails

I have a new app, and I want share the common part with another ror app.
For example, I have a MVC named showcase for showing images in the previous app, I want have the same one in my new app too.
Now I have connect my new app with the previous app's database, create a model with same name"showcase", and could get its columns like image_file_name; image_content_type;; image_file_size.. but in the view page for showcase in the new app, it can not recognize imagewhich is paperclip type attribute.
Can anyone tell me how can I use the images from the previous app?
Thanks a lot!
update:
class Showcase < ActiveResource::Base
self.site = "http://localhost:3000"
end
controller:
class ShowcasesController < ApplicationController
def index
#showcases = Showcase.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #showcases }
format.xml { render :xml => #showcases }
end
end
def show
#showcase = Showcase.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #showcase }
format.xml { render :xml => #showcases }
end
end
end
view:
<%= image_tag #showcase.image.url %
the error:
undefined method"image" for #
how could my view recognize image?
by the way in the original app, it didn't render xml, so I manually append render xml is that ok?
format.html # show.html.erb
format.json { render json: #showcase }
format.xml { render :xml => #showcases }
If you have a proper restful API to access the showcase resource in your first app(app1), then you can use the showcase model from the other ROR app(app2) ActiveResource. ActiveResource abstracts the communication between the two applications and it looks as though the model belonged to the same application.
If the restful url for showcase model in app1 is www.app1.com/showcases, then in the app2, all you have to do is create a model named showcase like below.
class ShowCase < ActiveResource:Base
self.site="www.app1.com"
end
Check out rails cast episode http://railscasts.com/episodes/94-activeresource-basics for more details.

Resources