External link to controller won't render .js.erb file Rails 4 - ruby-on-rails

I can render a .js.erb file following an ajax request, but if I type this url into the browser:
http://localhost:3000/posts/11
with a Post model that has a show action defined as
def show
respond_to :js
end
and a corresponding show.js.erb file, I get the following error:
ActionController::UnknownFormat at /posts/11
ActionController::UnknownFormat
I want to be able to generate links for users to copy and paste so that they can link to posts, but I can't get passed this error.

You need to specify format within your url:
http://localhost:3000/posts/11.js
To generate such a route "rails way" pass format option to path:
link_to post.title, post_path(id: post.id, format: :js)

Related

Download and redirect in rails

I have to put a link to download pdf file on my Rails app and it works.
But after download I don't want users in .../download_pdf.fr page but I want to redirect them in index.
I tried to put redirect_to root_path in download method in my controller but download didn't work with that.
If you have any idea.
Thank you a lot
Routes
get 'download_pdf', to: "home#download_pdf"
Controller
def download_pdf
send_file "#{Rails.root}/app/assets/images/file.pdf", type: "application/pdf", x_sendfile: true
end
View
<%= link_to "Télécharger", download_pdf_path, class:"button1" %>

ActionController::UnknownFormat in HelpController#about

as a Ruby newbie I am still getting to grips with the language. I have created a broadcast controller for a simple database that is already being used in production. However, I am getting the above mentioned error. Below is the code I have used:
show.html.erb
<p id="notice"><%= notice %></p>
<%= link_to 'Edit', edit_broadcast_path(#broadcast) %> |
<%= link_to 'Back', broadcasts_path %>
Index.html.erb
index.html.erb
broadcasts_controller.rb
boradcast controller
help controller
class HelpController < ApplicationController
skip_before_action :authenticate_user!
def about
#render text: "Hello"
end
end
I am not sure if I am missing any files or configs, I will add them in the comments if need be. Thanks
Incoming requests may use headers or parameters to indicate to Rails what format, called "MIME type", the response should have. For instance, a typical GET request from entering a URL into your browser will ask for an HTML (or default) response. Other types of common responses return JSON or XML.
In your case your "about" action does not have any explicit responders, and because of that Rails can't match the requested format (which is what the error message is trying to convey). You will probably just want to add an HTML template app/views/help/about.html.erb with your content. Rails should identify the HTML template and handle things from there.
More info
In Rails you need to respond with a specific format, and it is easy to setup your controller actions to handle a variety of formats.
Here is a snippet you might find in a controller which can respond in 3 different ways.
respond_to do |format|
format.html { render "foo" } # renders foo.html.erb
format.json { render json: #foo }
format.xml { render xml: #foo }
end
You can see more examples and deeper explanations in the documentation here.
ActiveRecord helps because it comes with serializers out of the box which can create JSON and XML representations of your objects.

No route matches [GET] "/controller/method"

I am trying to call a controller action on the link on rails app.
The controller action basically creates a new phone number and generates a Twilio pin.
controller
def resend
#phone_number = PhoneNumber.find_or_create_by(phone_number: params[:phone_number][:phone_number])
#phone_number.generate_pin
#phone_number.send_pin
respond_to do |format|
format.js # render app/views/phone_numbers/create.js.erb
end
end
view
Resend Pin
routes.rb`
post 'phone_numbers/resend' => "phone_numbers#resend"
So when I click "Resend Pin". I am getting
No route matches [GET] "/phone_numbers/resend"
rake routes output
phone_numbers POST /phone_numbers(.:format) phone_numbers#create
new_phone_number GET /phone_numbers/new(.:format) phone_numbers#new
phone_numbers_verify POST /phone_numbers/verify(.:format) phone_numbers#verify
phone_numbers_resend POST /phone_numbers/resend(.:format) phone_numbers#resend
In routes, I have set it as a post. Why am I getting this? How can I fix this?
link_to "Send Pin", phone_numbers_resend_path, method: :post
Anchor tag(<a>) by default uses get request . But in routes, you are using post method. So, to make it working you can do any one of the below:
<%= link_to 'Resend Pin', phone_numbers_resend_path, method: :post %>
or
get 'phone_numbers/resend' => "phone_numbers#resend"

How do I add different types of GET routes that require parameters in Ruby on Rails

I have a list of users being displayed, you can click on "Show user" or "PDF" to see details of that user in HTML or as a PDF document. The show was automatically created with scaffolding, now I'm trying to add the option to view it as a PDF. The problem is adding a second GET option, if I pass the user along as a parameter, it is assumed to be a POST and I get an error that the POST route does not exist. I am not trying to update the user, just to show it in a different way, basically to add a second "show user" option.
How do I tell it that I want a GET, not a POST? Is there an easier way to do what I am trying to do? Thanks.
Please, create a controller like this:
class ClientsController < ApplicationController
# The user can request to receive this resource as HTML or PDF.
def show
#client = Client.find(params[:id])
respond_to do |format|
format.html
format.pdf { render pdf: generate_pdf(#client) }
end
end
end
Please, update route.rb file, action name with post and get, like below :
match 'action_name', to: 'controller#action', via: 'post'
match 'action_name', to: 'controller#action', via: 'get'
More info please read this link : "http://guides.rubyonrails.org/routing.html"
you haven't posted any code or details, so I am guessing you want something like this:
routes
resources :users
controller
class UsersController < ActionController::Base
def show
#user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.pdf # handle the pdf response
end
end
end
view file in views/users/show.pdf.prawn
prawn_document() do |pdf|
#user.each {|r| pdf.text r.id} # will print user id of the user
end
The way above example will work is, if something visits the following URLs, they will get html file:
localhost:3000/users/1 #html is the default format in rails
localhost:3000/users/1.html
but if they visit .pdf, they will be served a pdf format.
localhost:3000/users/1.pdf
If the above assumptions are correct, then check prawn or wicked_pdf pdf gem. the above example uses prawn
Checkout this link http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to. You can add a new MIME type and pass on the :format as pdf in all your rails routes.
Hope this will help.
And for the POST-request check your
config/routes.rb
There shoud be a few routes already, so you can infer the route you need.
In your link you can pass an additional parameter called format for pdf. For e.g.
<%= link_to 'Display in PDF', "/user/pdf", :format => "pdf" %>

where does the url routes get resolved when you call render in rails controller actions

Is there any method that i should look at in rails3.2 source code so as to know where the navigation or the url part of the render call get resolved?
The reason is, i have a small app in which url is of the form
www.example.com/bob/edit
the above route as it suggests renders the edit form.EDIT: i was able to get to this route by modifying response on the link_to helper.
def update
#when validation passes
redirect_to #user
#when validation fails
respond_to do |format|
format.html {render :action => "edit"}
end
end
Now the problem is when a validation error occurs on submission to update action of users_controller,
the url becomes
www.example.com/users/bob/edit
config/routes.rb
get "users/new", to: => "users#new"
resources :users
as you can see there's nothing interesting happening in routes,
in models/user.rb
def to_param
"#{name}"
end
in views/edit.html.erb
form_for(#user) do |f|
end
Observation: here when the form is rendered afresh, form 'action' points to "users/bob" but when the form is re-rendered 'cos of validation error, form action mysteriosly changes to "users/" which is weired and if i remove the to_param in user.rb model it works fine
Though its not such a big deal, i was thinking where, if i needed to override the url that is generated on render call, to change?????
Any suggestions and pointers to explore are wecome....
I'm not sure how you're getting the URLs you're getting, but a general answer to your question would be it doesn't. The URL you see after sending a request is the URL the request was sent to (or redirected to), not that of the page you came from, nor that of the template you render in the end. In your case, I'm guessing the problem is that you created a custom URL for the edit page, but not for update, and your form_for(#user) is sending the request to your update URL (probably PUT "/users/bob").
To fix this, the first thing is to create your custom update route. Maybe something like:
put ":id/update", to: => "users#update"
And then have your form_for use that URL:
form_for(#user, :url => "#{#user.to_param}/update")

Resources