Ruby On Rails - Pass parameters from front end to database through controllers - ruby-on-rails

How to pass parameters from front end to back-end API in Ruby on Rails only through controllers? I do not want to use model or views for this.
I am using a Ruby Gem which captures some usage data which needs to be stored into the back end database.
I have created a controller, to which the post parameters are sent, but I get an error saying view is not found.
ActionView::MissingTemplate (Missing template usage_metrics/create, application/create with {:locale=>[:en], :formats=>[:json, :js, :html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :web_console_v2], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/home/local/www/cc2/cc_user-frontend/app/views"
* "/home/local/.rvm/gems/ruby-2.2.3/gems/ckeditor-4.2.2/app/views"
):

You need to send the request in an appropriate format (i.e. json) and ensure the controller action responds to this. At the moment it's trying to respond with html but not finding a template for this.
For example, in the action, after you've done what you need to do:
respond_to do |format|
format.json { render json: { whatever: is_needed_in_the_response } }
end
And you'll get your response that way.
Or if you don't need a response after parsing the params, you can use render nothing: true.
N.B. I think the newest versions Rails will respond with nothing / json if there's no template found. Not sure how this affects you.
Hope that helps!

if you want to return some data as json you can use
render json: data
but if you only want to return status
render json: {}, status: 200

Related

Rails Template is missing render json

actually i use rails for my REST API, and i need transform my object to json but when i try i got this error:
<h1>Template is missing</h1>
<p>Missing template firms/show, application/show with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :arb, :jbuilder]}. Searched in:
* "/Users/allan/Desktop/Work/back/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/activeadmin-0.6.0/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/kaminari-0.16.3/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/devise_invitable-1.5.5/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/devise-3.5.4/app/views"
</p>
This is my code
def show
firm= Firm.find_by_subdomain(params[:subdomain])
if firm.present?
respond_to do |format|
#firm = firm
format.json { render :json => #firm.to_json }
end
end
end
I hope someone here can help me :)
Solve:
def show
render json: Firm.find_by_subdomain(current_subdomain)
end
thank you
Template missing means that you asking for a html view, not doing a json request.
If you want to always return json format regardless of format param, do this:
before_action :set_default_response_format
protected
def set_default_response_format
request.format = :json
end
#source: Rails 4 - How to render JSON regardless of requested format?
Try to add .json at the end of you query when querying your route. Without this, it will try to render html and it will go search for view file that might not be present in your case.

Render js.erb from controller

I have a controller action that I am trying to only render js from. Here's what I have
def get_script
respond_to :js
render :script
end
I'm using respond_to :js to hopefully force the request to only respond to js. Then I'm calling render :script to load a file called script.js.erb
My request is the following
Completed 500 Internal Server Error in 15ms (ActiveRecord: 0.5ms)
ActionView::MissingTemplate (Missing template widgets/get_script, application/get_script with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder]}. Searched in:
* "/home/jkoehms/TECC/tecc/app/views"
* "/home/jkoehms/.rvm/gems/ruby-2.0.0-p643/gems/devise-3.5.1/app/views"
)
So there are two problems here. Although the request is processed as JS, the render is looking for js or html as indicated in the formats section. Secondly, the render is looking for get_script.js.erb when it should be looking for script.js.erb. I used the following documentation as a resource for rendering: Layouts and Rendering
Question:
1.) Does the respond_to :js do what I'm hoping it to do, or do I have to put it in a do |format| block?
2.) Why isn't render :script looking for script.js.erb?
Have you tried this inside your view?
render "widgets/script.js.erb", format: :js

Rendering a view of controller action from around_action callback

I'm rendering a js.erb partial which enables ajax functionality to like/dislike a restaurant dish. I recently came across the around_action callback and figured yield would help perform the controller action first and render the template second. Unfortunately I'm getting a 500 (Internal Server Error) due to the respond_to never getting called.
The respond_to method works if I place it inside the controller action but not inside the callback. What am I doing wrong?
class DishesController < ApplicationController
before_action :set_dish_and_restaurant
around_action :render_vote_partial
def like
#dish.liked_by current_user
end
...
private
def set_dish_and_restaurant
#dish = Dish.find(params[:id])
end
def render_vote_partial
yield
respond_to { |format| format.js { render "vote.js.erb" } }
end
end
Console Error
ActionView::MissingTemplate (Missing template dishes/like, application/like with {:locale=>[:en], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in:
* "/app/views"
* "/Library/Ruby/Gems/2.0.0/gems/devise-3.5.1/app/views"
):
app/controllers/dishes_controller.rb:29:in `render_vote_partial'
Okay so with your stack trace it is pretty clear what is happening. You have to understand the default rails behavior of convention over configuration.
As soon as you call yield, your controller action gets called. Now all controller actions by default look to render views with the same name as action, once the actions are done executing.
So calling render_to after yield doesn't make any sense, as controller action you yielded to has already called its render :)
In any case what you are trying to do is a bad design pattern, rendering views should be left to actions
Update
Theoretically speaking : As you wish to keep things DRY you could render the same view after each action by creating a common method calling it after every action. However, think about it, your render will have one line, and calling that same method will need one line too :) so where's the DRY.
In short, DRY should not be over done at the cost of simplicity. In my opinion KISS trumps DRY :)

Missing template action error in rails3

I have defined a method in controller that is like:
def self.dailymail
.... #fill data from db
ac = ActionController::Base.new()
kit = PDFKit.new(ac.render_to_string(:action => "formatinhtml.html.erb",:rawdata => data))
pdf = kit.to_pdf
... #send pdf in mail
end
formatinhtml is like:
def formatinhtml
#dailyrep = params[:rawdata]
respond_to do |format|
format.html # daily.html.erb
end
end
I have to use self.dailymail so that I can call it from model & in turn from rufus scheduler.But,still I get error such as:
scheduler caught exception:
Missing template action_controller/base/daily.html with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :xls], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "F:/DEVELOPMENT/TrackIt/app/views"
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_view/path_set.rb:58:in `find'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.8/lib/action_view/lookup_context.rb:109:in `find'...
so,what does I need to do?
Update: After debugging,I found action formatinhtml is not actually being getting called;I have defined neccessary routes.
Your folder where "daily.html" is located is not the same as your controller

Is it possible to render a js.erb without using the respond_to method and using an unconventional filename?

Is it possible to render a js.erb in response to an AJAX request without using the respond_to method?
I am asking because of a few reasons:
I will only be making AJAX calls to my controllers;
I will not be supporting any other content types (i.e., :html, :xml, etc.); and
I want to be able to render a different js.erb file, not one named the same as the controller method (i.e., different.js.erb)
Here is some code to serve as an example.
app/views/posts/awesome.js.erb:
alert("WOOHOO");
PostsController#create
def create
#post = Post.new(params[:task])
if #post.save
render :partial => 'awesome.js.erb'
end
end
When the create method is called via AJAX, Rails complains about a partial missing:
ActionView::MissingTemplate (Missing partial post/awesome.js, application/awesome.js with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:js, "application/ecmascript", "application/x-ecmascript", :html, :text, :js, :css, :ics, :csv, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json], :locale=>[:en, :en]}. Searched in:
While Kieber S. answer is correct, here is another method that I think would be valuable for those who want to support the "conventional" method of creating js.erb files.
Instead of using render :partial use render :template. The caveat here, however, is that the search path for the template isn't automatically scoped to the name of the controller (i.e., app/views/posts). Instead, Rails starts at app/views/. So to reference the "template", you need to add the subfolder you want to look into, if it isn't in the root. In my case, I had to add posts.
def create
#post = Post.new(params[:task])
if #post.save
# using :template and added 'posts/' to path
render :template => 'posts/awesome.js.erb'
end
end
The benefit here is that if you should so happen to want to use the respond_to method and follow convention, you wouldn't need to rename your js.erb by removing the underscore.
Partial files need to be named as with a underscore.
Try to rename your partial to app/views/posts/_awesome.js.erb

Resources