So here's my controller:
class ScriptController < ApplicationController
respond_to :js
def show
puts 'here'
#client_id = params[:id]
respond_with #client_id
end
end
I have a file called in app/views/script/show.js.coffee but it doesn't load when I go to the show page. I'm using the coffeebeans gem: Loading .coffee files via a view in Rails
Any help?
EDIT: Added Error
Template is missing
Missing template good_comments/script/show, good_comments/application/show with {:handlers=>[:erb, :builder], :formats=>[:js, :html], :locale=>[:en, :en]}. Searched in: * "/Users/shamoon/Sites/good_comments/spec/dummy/app/views" * "/Users/shamoon/Sites/good_comments/app/views"
Don't use CoffeeBeans, coffee-rails handles rendering of coffeescript out-of-box.
I render coffeescripts like this:
respond_to do |format|
format.html { redirect_to '/' }
format.js { render 'delete', :layout => false }
end
Related
I am trying to create a .pdf that lists all of my projects(#index).
I found a great link-How do generate PDFs in Rails with Prawn, however it was from 2008 and wanted me to use the prawnto plugin.
I am using Rails 3.2.13 so I decided to use the gem prawn and RailsCast #153 PDFs with Prawn (revised), for reference. I was able to successfully get Prawn working in my:
projects_controller
def show
I am having trouble getting the .pdfs working in my def index though.
I tried to just mimic what I did, using the tutoiral for def show, for def index but am getting a routing error.
Here is my code thus far:
Gemfile
gem 'prawn', '0.12.0'
projects_controller.rb
class ProjectsController < ApplicationController
def index
redirect_to action: :active, search =>params[:search]
end
def active
#action = "active"
....
.... // search code
.... // kaminari code
#projects = Project.order(sort_column + "" + sort_direction)
respond_to do |format|
format.json { render "index" }
format.html { render "index" }
format.pdf do
pdf = ProjectAllPdf.new(#projects)
send_data pdf.render, filename: "project_#{#project.product}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
def show
#project = Project.find(params[:id])
respond_to do |format|
format.json { render json:#project }
format.html # show.html.erb
format.pdf do
pdf = ProjectPdf.new(#project)
send_data pdf.render, filename: "project_#{#project.product}.pdf",
type: "application/pdf",
disposition: "inline"
end
end
end
end
show.html.erb
<p><%= link_to "Printable Receipt (PDF)", project_path(#project, format: "pdf") %></p>
index.html.erb
<p><%= link_to "Printable Receipt (PDF)", projects_path(#projects, format: "pdf") %></p>
I then formatted my file
project_pdf.rb
class ProjectPdf < Prawn::Document
def initialize(project)
super(top_margin: 70)
#project = project
overview_print
end
def overview_print
text "Project #{#project.product}", size: 24, style: :bold, align: :center
move_down 30
text "<b>Product:</b> #{#project.product}", :inline_format => true
move_down 8
text "<b>Version Number:</b> #{#project.version_number}", :inline_format => true
move_down 8
....
....
end
end
I then tried to mimic the last file to get #index working
projectall_pdf.rb
class ProjectAllPdf < Prawn::Document
def initialize(project)
super(top_margin: 70)
#project = project
overview_print
end
def overview_print
#projects.each do |project|
text "<b>Product:</b> #{#project.product}", :inline_format => true
move_down 8
text "<b>Version Number:</b> #{#project.version_number}", :inline_format => true
move_down 8
....
....
end
end
end
Everything works great for #show. I just obviously have gotten myself mixed up on how to do the #index portions (def active, linking the .pdf in index.html.erb and projectall_pdf.rb)
I thought I would post an answer to my question, hopefully it helps somebody.
I actually went ahead and used the 'gem prawnto_2', :require => "prawnto"
It allowed me to use the prawnto and the prawnto tutorial with Rails 3.2
I then just created (method).pdf.prawn pages in my app/views/projects folder.
Then just add your custom pdf code to have you want to layout your pdf views.
I have a UsersController that has index and new actions. I use haml, the index haml file contains the following code:
= link_to 'Add User', new_user_path, { :remote => true, 'data-toggle' => 'modal',
'data-target' => '#modal-window', 'class' => 'btn btn-primary pull-right' }
So when the user clicks 'Add User' the _new.html.haml partial is presented to the user as a modal. It works great. Here's the new action in the controller:
def new
#user = User.new
respond_to do |format|
format.html
format.js
end
end
Now in my controller spec I am trying to do the following:
describe "new action" do
before do
get 'new'
end
# specs for 'new' action go here
end
However that gives an error
Failure/Error: get 'new'
ActionView::MissingTemplate:
Missing template users/new, application/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
* "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0x007faa44b03218>"
Presumably because it can't find new.html.haml which of course doesn't exist because the file is a partial named _new.html.haml. What is the correct syntax for getting the partial? If not, how can I test the new action?
Okay, here's what I changed in the new action to make it work:
respond_to do |format|
format.html { render :partial => 'new' }
format.js
end
When the app is running rails figures out to render the partial, but I guess it needs to be explicit for rspec. If there are better ways to do it I'd be glad to hear them.
If I want to go with my home page clicking on the map localhost:3000/maps gets out this error No route matches {:action=>"show", :controller=>"restaurants"}
controllers/maps_controller.rb
def index
#maps = Map.all
#json = Map.all.to_gmaps4rails do |map, marker|
marker.infowindow info_for_restaurant(map.restaurant)
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #maps }
end
end
def show
#map = Map.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #map }
end
end
private
def info_for_restaurant(restaurant)
link_to restaurant_path do
content_tag("h2") do
restaurant.name
end
end
end
routes.rb
resources :restaurants
resources :maps
This is answer for my question:
controllers/maps_controller.rb
def index
#maps = Map.all
#json = Map.all.to_gmaps4rails do |map, marker|
marker.infowindow render_to_string(:partial => "/maps/maps_link",
:layout => false, :locals => { :map => map})
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: #maps }
end
end
views/maps/_maps_link.html.erb
<div class="map-link">
<h2><%= link_to map.restaurant.title, map.restaurant %></h2>
</div>
You referred to restaurant_path within info_for_restaurant, which is part of MapsController. Rails met error here.
You need to either define the restaurant_path in restaurant controller, or comment out this function in maps controller at this moment.
Your approach is wrong in several levels. Let's work on them, one at a time:
1) Your call to the route helper is wrong:
restaurant_path is the route helper for a show action. A show action needs an id parameter to be valid. Your call is missing a parameter.
So, your code must be something like this:
def info_for_restaurant(restaurant)
link_to restaurant_path(restaurant) do
content_tag("h2") do
restaurant.name
end
end
end
To see the parameters needed for each action, you can run rake routes on the console.
However, this does not solve the problem, as you're also:
2) Calling view helpers from your controller
link_to and content_tag are view helper methods, and you don't want to bother your controller with view issues. So, the best way to solve this problem is to move your info_for_restaurant method to a helper, and call it from a view instead.
So, now, your controller will not assign anything to #json, and the last line of your view will look like this:
<%= gmaps4rails #maps.to_gmaps4rails {|map, marker| marker.infowindow info_for_restaurant(map.restaurant) } %>
What is the best approach here? I'm trying to clean up some code and I'm wondering if the controller is the best place for this variety of logic:
if user_signed_in?
if current_user.try(:admin?)
#docs = Doc.chronologic.page(params[:page]).per(5)
#orders = Order.chronologic.page(params[:page]).per(5)
else
#docs = Doc.chronologic.where(:user_id => current_user.ftp, :retired => "active").page(params[:page]).per(5)
#orders = Order.chronologic.where(:user => current_user.ftp).page(params[:page]).per(5)
end
respond_to do |format|
format.html
format.json { render json: #docs }
end
else
redirect_to new_user_session_path
end
If there's a better location for it, where would it be?
Thanks!
Edit: it's far worse for methods like pdf which has line after line of instructions for Prawn, but I can't seem to get send_data to work from the model.
This is basically what mu said, but here's my take.
In your app controller:
def require_logged_in
redirect_to new_user_session_path unless user_signed_in?
end
In your controller
before_filter :require_logged_in
def some_action
#docs = Doc.chronologic.for_user(current_user).page(params[:page]).per(5)
#orders = Order.chronologic.for_user(current_user).page(params[:page]).per(5)
respond_to do |format|
format.html
format.json { render json: #docs }
end
end
In your Doc model
scope :for_user, lambda do |user|
where(:user_id => user.ftp, :retired => "active") unless user.admin?
end
And something similar in your Order model.
Per your edit, definitely don't do send_data from your model.
I have the following in my controller:
respond_to :html, :except => :some_action
respond_to :json, :xml
If you hit the :some_action route in a browser (tested with Chrome), you get a 406 Not Acceptable response back. Is there a way to "catch" this in Rails and do something else (like a redirect)?
Additionally, I'm trying to avoid using the block form of respond_to. I'm just curious if there is some way to handle this case.
Check this out: http://ryandaigle.com/articles/2009/8/6/what-s-new-in-edge-rails-cleaner-restful-controllers-w-respond_with
There's a bit about action overriding:
class UsersController < ApplicationController::Base
respond_to :html, :xml, :json
# Override html format since we want to redirect to a different page,
# not just serve back the new resource
def create
#user = User.create(params[:user])
respond_with(#user) do |format|
format.html { redirect_to users_path }
end
end
end