I am currently learning Rails Guides. I went through the steps but still encountered a mistake.
My Ruby version is ruby 2.1.1p76 and the Rails version is 4.0.4.
As the guide directed, I created an Article Controller.
class ArticlesController < ApplicationController
def new
end
def create
render plain: params[:article].inspect
end
end
I should get {"title"=>"First article!", "text"=>"This is my first article."} but the output turned out to be
Template is missing
Missing template articles/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.`
Here is my related routes:
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
Update: render plain: is a new method introduced in Rails 4.1.0 referred to this issue.
In the render method, plain option was added in Rails 4.1 and you are using Rails 4.0.4. So, rails ignored this option and started looking for a template named articles/create as you are in ArticlesController#create action. Obviously, the template doesn't exist so you get the error Template is missing.
Refer to the discussion on this topic on Github: Introduce render :plain and render :html, make render :body as an alias to render :text
Now, for using the below mentioned syntax you would need to upgrade to Rails 4.1:
render plain: params[:article].inspect
With your current version of Rails 4.0.4, you can go for:
render text: params[:article].inspect
If you want to see textual information of params[:article] on your page then you can use render text
try this
class ArticlesController < ApplicationController
def new
end
def create
render text: params[:article].inspect
end
end
You will get
{"title"=>"First article!", "text"=>"This is my first article."}
# i.e. your params(whatever params hash contains)
Change Rails version in your Gemfile:
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
Then run:
bundle install
Make sure that your Rails version now is > 4.1
You no need template means you could use render nothing: true
Try like this:
class ArticlesController < ApplicationController
def new
end
def create
params[:article].inspect
render nothing: true
end
end
Please refer this link click here
You may want to read through the following documentation.
Rendering pure text is most useful when you're responding to Ajax or
web service requests that are expecting something other than proper
HTML.
Related
I need to create a .docx file from a HTML template, so I used htmltoword gem.
Usage:
I added the gem (Gemfile):
gem 'htmltoword', '~> 0.5.1' #last version of the gem
I put a route (route.rb):
get 'preview' => 'foo#preview'
And in my bar.html.erb I have a link which target's that url:
<%= link_to '.docx', preview_path %>
Template (preview.docx.erb):
<h1>foobar</h1>
And in the controller (foos_controller.rb):
class FoosController < ApplicationController
respond_to :docx
#other code
def preview
respond_to do |format|
format.docx do
render docx: 'foobar', filename: 'preview.docx'
end
end
end
end
However, I'm getting an error:
ActionController::UnknownFormat
How to fix this error?
My config:
RoR v4.2.4
Ruby v2.2.3p173
Also, there is an open github issue for this/similar topic.
Update: as #kajalojha mentioned, respond_with / Class-Level respond_to has been removed to an individual gem, so I installed the responders gem, however, I get the same error.
Since respond_to has been removed from rails 4.2 to a individual gem i will recommend you to use formatter gem..
For further details you can look to the link given below.
Why is respond_with being removed from rails 4.2 into it's own gem?
Have you tried caracal-rails? You can find it here
I had to build this same functionality in an app earlier this year and also used the htmltoword gem.
# At the top of the controller:
respond_to :html, :js, :docx
def download
format.docx {
filename: "#{dynamically_generated_filename}",
word_template: 'name_of_my_word_template.docx')
}
end
I then have two "view" files that come into play. The first, is my method view file download.docx.haml. This file contains the following code:
%html
%head
%title Title
%body
%h1 A Cool Heading
%h2 A Cooler Heading
= render partial: 'name_of_my_word_template', locals: { local_var: #local_var }
From there, I have another file name_of_my_word_template.docx.haml that contains the meat of my Word file.
%h4 Header
%h5 Subheader
%div= local_var.method
%div Some other content
%div More content
%div Some footer content
When someone hits my_app.com/controller_name/download.docx, a Word file is generated and downloaded for them.
In order to ensure this happens, I have a route for the download method in my routes.rb file:
resources :model_name do
member do
get :download
end
end
Apologies for the long reply ... this has worked well for me and I hope helps you through this issue!
So, I figured it out. I added format: 'docx' to the route and it works now.
Note: as #kajalojha mentioned, respond_with / Class-Level respond_to has been removed to an individual gem, so I installed the responders gem.
Let's create a download logic.
Gemfile
gem 'responders'
gem 'htmltoword', '~> 0.5.1'
routes.rb
get 'download' => 'foos#download', format: 'docx' #added format
foos_controller.rb
class FoosController < ApplicationController
respond_to :docx
def download
#bar = "Lorem Ipsum"
respond_to do |format|
format.docx do
# docx - the docx template that you'll use
# filename - the name of the created docx file
render docx: 'download', filename: 'bar.docx'
end
end
end
end
download.docx.erb
<p><%= #bar %></p>
And I've added some link to trigger the download logic:
<%= link_to 'Download bar.docx', foo_download_path %>
Which will download the bar.docx file with "Lorem Ipsum" in it.
I am trying to install wicked_pdf in order to generate pre-filled contracts between 2 users on my Rails application.
I feel like I have installed wicked_pdf properly, but I get an "ActionController::UnknownFormat" error.
What I did :
# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
With or without uncommenting the 'exe...' lines (one after the other), I still get the error :
# initializers/wicked_pdf.rb
WickedPdf.config = {
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: '/usr/local/bin/wkhtmltopdf',
# or
# exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.html',
}
My controller:
#bookings_controller.rb
class BookingsController < ApplicationController
def show
#booking = Booking.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render pdf: "test_wicked_pdf"
end
end
authorize #booking # For Pundit
end
The render HTML is working when i go to localhost:3000/bookings/135 ...
# bookings/show.html.erb
<h1>PDF test</h1>
...but not the PDF when I comment out "# format.html" in my controller
# bookings/show.pdf.erb
<h1>PDF test</h1>
Thanks a lot in advance
I know it is a pretty old question but you can use the gem wkhtmltopdf-binary-edge instead of the regular wkhtmltopdf-binary gem. It worked for me.
I'm trying to learn how to use AJAX in my rails apps so i've decided to start with something simple. I have a blog app on which user can vote on any blog post. Here is my code for posts#vote:
posts_controller.rb
class PostsController < ApplicationController
(...)
def vote
post = Post.find(params[:id])
if current_user.voted_on?(post)
current_user.unvote_for(post)
else
current_user.vote_for(post)
end
respond_to do |format|
format.html { redirect_to post_path(post) }
format.js
end
end
end
and here is a link code for my posts#view:
view.html.erb
<%= link_to "Vote", vote_post_path(post.id), :remote => true %>
And now, if i'll click my Vote link, posts#vote action works and vote is casted, however i'm getting an error:
ActionView::MissingTemplate (Missing template posts/vote,
application/vote with {:handlers=>[:haml, :coffee, :erb, :builder],
:locale=>[:en], :formats=>[:js, :html]}.
I have (empty) vote.rjs file in my views/posts folder but for some reason rails can't see it. According to error, the only file extensions that rails is searchng for are .haml, .coffee, .erb and .builder. Shouldn't there be also a .rjs extension on that list? Thanks in advance.
Your file should be called vote.js.erb. Rails doesn't use a .rjs extension.
The .rjs extension was originally used for Rails and Prototype JS library.
With Rails 3.1 the default library was switched to JQuery.
If you want to use .rjs in your Rails project it would mean using Prototype instead of JQuery. The gem for that is prototype-rails.
I get this when I try and run my view:
Missing template application/login with {:formats=>[:html], :locale=>[:en], :handlers=>[:coffee, :erb, :builder]}. Searched in: * "/home/carladessi/Goods In Final/app/views"
in my controller I have:
def login
# respond_to do |format|
# format.html
end
and in my routes I have:
match "/login/", :controller => 'application', :action => 'login'
I'm guessing I need to put something else in the controller I just don't know what.. sorry if this is a really blatant question!
Restarted the server and it works fine !
It is not really conventional Rails to render views from the application_controller.
However, what is happening is Rails is looking for an actual template or view to be here:
RAILS_ROOT/app/views/application/login.html.erb
What you can do is add/create that template at the above path. Or you can redirect to another controller (which exists and does render an actual template).
I'm working with rails 3.1.0 and this is my first application on 3.1.0
I have a remote link:
link_to "my link",{:controller=>"my_controller",:action=>"my_action"},:remote=>true
and in my_controller I have
def my_action
#data = Data.all
render :update do |page|
page.replace_html "show_data",:partial=>"data_partial"
end
end
but then in the log I get an error
ActionView::MissingTemplate (Missing template my_controller/update...
and I was checking at this post
http://wowkhmer.com/2011/09/19/unobtrusive-ajax-with-rails-31/
do I really need to use a coffee script or a js.jrs to do this thing ??
Javascript integration doesn't work this way anymore. render :update ... tries to render the update action, which doesn't have an associated template. You need to move that out of the controller and into the view code, in app/views/my_controller/my_action.js.erb:
$("show_data").update("<%= escape_javascript(render :data_partial) %>");