How to pass two object to prawn pdf - ruby-on-rails

I have following structure in my controller and i want to pass two object to generate pdf, below is my controller code but its not working.
def show
#customer = Customer.find(params[:id])
#orders = Orders.find(params[:id])
#pdf = Prawn::Document.new()
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #customer }
format.xml { render :xml => #orders}
format.pdf { render :layout => false }
pdf.render_file {path_to_file}
end
I also tried #customers.orders = #orders but it didn't work.
Is there any way to get it done ?

Related

Routing to contents of model failing due to no ID match

I am relatively new to Rails and have recently managed to break the links to the contents of one of my models...
Having previously posted a question here on stackoverflow, I adjusted the to_param function in my model, such that the product name would be appended to the product ID.
The changes I made were:
In products.rb,
def to_param
"#{id}-#{product_name.parameterize}"
end
In routes.rb,
match '/:id' => 'uniquewetsuits#show'
This successfully creates the address I am hoping for /products/ID-product-name, however, I get an error stating there is no product with ID=ID-product-name.
If I navigate to /products/ID, I can successfully view the page as normal.
Can anybody inform me as to how I go about reconnecting things so that I do get a match for the longer ID string?
Thanks for your time
EDIT
The contents of the controller are:
def index
##search = Uniquewetsuit.search(params[:q])
##findwetsuits = #search.result(:distinct => true)
#if #findwetsuits.count > 0
# #uniquewetsuits = #findwetsuits.all
#else
#uniquewetsuits = Uniquewetsuit.all
#end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #uniquewetsuits }
end
end
# GET /uniquewetsuits/1
# GET /uniquewetsuits/1.xml
def show
#uniquewetsuit = Uniquewetsuit.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #uniquewetsuit }
end
end
# GET /uniquewetsuits/new
# GET /uniquewetsuits/new.xml
def new
#uniquewetsuit = Uniquewetsuit.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #uniquewetsuit }
end
end
# GET /uniquewetsuits/1/edit
def edit
#uniquewetsuit = Uniquewetsuit.find(params[:id])
end
# POST /uniquewetsuits
# POST /uniquewetsuits.xml
def create
#uniquewetsuit = Uniquewetsuit.new(params[:uniquewetsuit])
respond_to do |format|
if #uniquewetsuit.save
format.html { redirect_to(#uniquewetsuit, :notice => 'Uniquewetsuit was successfully created.') }
format.xml { render :xml => #uniquewetsuit, :status => :created, :location => #uniquewetsuit }
else
format.html { render :action => "new" }
format.xml { render :xml => #uniquewetsuit.errors, :status => :unprocessable_entity }
end
end
end
# PUT /uniquewetsuits/1
# PUT /uniquewetsuits/1.xml
def update
#uniquewetsuit = Uniquewetsuit.find(params[:id])
respond_to do |format|
if #uniquewetsuit.update_attributes(params[:uniquewetsuit])
format.html { redirect_to(#uniquewetsuit, :notice => 'Uniquewetsuit was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #uniquewetsuit.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /uniquewetsuits/1
# DELETE /uniquewetsuits/1.xml
def destroy
#uniquewetsuit = Uniquewetsuit.find(params[:id])
#uniquewetsuit.destroy
respond_to do |format|
format.html { redirect_to(uniquewetsuits_url) }
format.xml { head :ok }
end
end

How to create a new object in Rails with a predefined property

I have a Rails app that has a bunch of pages, each page has many convos. On each page there's a link to create a new convo on that page. This is the code for that link:
<%= link_to 'New Convo', new_convo_path(:page=>#page) %>
However, on the next page, "convo/new" the page property is empty. What am I missing?
EDIT here are my new and create functions for convos
def new
#convo = Convo.new(params[:page])
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #convo }
end
end
# POST /convos
# POST /convos.xml
def create
#convo = Convo.new(params[:convo])
respond_to do |format|
if #convo.save
format.html { redirect_to(#convo, :notice => 'Convo was successfully created.') }
format.xml { render :xml => #convo, :status => :created, :location => #convo }
else
format.html { render :action => "new" }
format.xml { render :xml => #convo.errors, :status => :unprocessable_entity }
end
end
end
You need to load the page ... try to set a before filter ...
before_filter :find_page
private
def find_page
#page = Page.find(params[:page_id])
end
Then you build using nested resources
def new
#convo = #page.convos.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #convo }
end
end
def create
#convo = #page.convos.build(params[:convo])
.....
end
My guess is that you are missing a ":page=>"
def new
#convo = Convo.new(:page=>params[:page])
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #convo }
end
end

How can I use another view if a specific parameter is given?

I have an action called new:
def new
#bookmark = Bookmark.new
respond_to do |format|
format.html # new.html.erb
format.mobile
format.xml { render :xml => #bookmark }
end
end
Now, when the param[:widget] == "true", I want to use a lay-out other than application.html.haml, and I want to show another view than new.html.haml called new_widget.html.haml.
How can I do this? Thanks.
Something like this?
def new
#bookmark = Bookmark.new
if params[:widget] == "true"
render 'new_widget.html.haml', :layout => 'path/to/other/layout'
return
end
respond_to do |format|
format.html # new.html.erb
format.mobile
format.xml { render :xml => #bookmark }
end
end

Sqlite on rails problem with relations

I have a problem with relations while using sqlite3 on rails.
First i build my scaffolds,
add the references to migration files,
add belongs_to has_many to models
than get my database up and runinig with basic rake db:migrate command.
And then it doesn't work,
I guess there is a missing step which i cannot figure out :S
By the way i am tryng to implement the example talewiki on Building Dynamic Web 2.0
Websites with Ruby on Rails, i am at page 75.
The example is on mysql.
class GenresController < ApplicationController
# GET /genres
# GET /genres.xml
def index
#genres = Genre.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #genres }
end
end
# GET /genres/1
# GET /genres/1.xml
def show
#genre = Genre.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #genre }
end
end
# GET /genres/new
# GET /genres/new.xml
def new
#genre = Genre.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #genre }
end
end
# GET /genres/1/edit
def edit
#genre = Genre.find(params[:id])
end
# POST /genres
# POST /genres.xml
def create
#genre = Genre.new(params[:genre])
respond_to do |format|
if #genre.save
format.html { redirect_to(#genre, :notice => 'Genre was successfully created.') }
format.xml { render :xml => #genre, :status => :created, :location => #genre }
else
format.html { render :action => "new" }
format.xml { render :xml => #genre.errors, :status => :unprocessable_entity }
end
end
end
# PUT /genres/1
# PUT /genres/1.xml
def update
#genre = Genre.find(params[:id])
respond_to do |format|
if #genre.update_attributes(params[:genre])
format.html { redirect_to(#genre, :notice => 'Genre was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #genre.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /genres/1
# DELETE /genres/1.xml
def destroy
#genre = Genre.find(params[:id])
#genre.destroy
respond_to do |format|
format.html { redirect_to(genres_url) }
format.xml { head :ok }
end
end
end
The error is occurring on this line:
#genre = Genre.find(params[:id])
giving
ActiveRecord::RecordNotFound in
GenresController#show Couldn't find
Genre with ID=tales
That means that params[:id] has the value "tales" which is wrong. I'm guessing here, but I bet that somewhere in the form or elsewhere there is an attempt to do a GET on /genre/tales instead of /tales/genre/:id (where :id should be an integer). I'm also guessing you have a mapping in routes.rb like:
map.resources :tales, :has_many => :genres
I don't have a copy of the book you're following.

Where do I insert my Rails URL in the JQuery Autocomplete to reference the data set I want?

Where do I reference my controller (Rails) URL to show the dataset that I want in the autocomplete via JQuery? Here is my head:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.dimensions.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.js"></script>
<script>
$(document).ready(function(){
var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
$("#example").autocomplete(data);
});
</script>
Trying to figure out how to reference my controller or model (whichever I should) to get only records that are associated with a specific user_id. Just some frame of reference.
I have three tables, Users, Prospects, and Notes. Trying to set it up so that a specific user (user_id) can "add a note" and then use an autocomplete field to help "tag" to a prospect they have previously entered. I have already set up authentication and it is all working. JQuery seems to be getting me the closest. The head is above, and also I have uploaded jquery-1.3.2.js (though no reference to it yet as you can see in the head). Here is my prospects controller code:
class ProspectsController < ApplicationController
before_filter :login_required
# GET /prospects
# GET /prospects.xml
def index
#prospects = current_user.prospects
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #prospects }
end
end
# GET /prospects/1
# GET /prospects/1.xml
def show
#prospect = current_user.prospects.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #prospect }
end
end
# GET /prospects/new
# GET /prospects/new.xml
def new
#prospect = Prospect.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #prospect }
end
end
# GET /prospects/1/edit
def edit
#prospect = current_user.prospects.find(params[:id])
respond_to do |format|
format.html # edit.html.erb
format.xml { render :xml => #prospect }
end
end
# POST /prospects
# POST /prospects.xml
def create
#prospect = current_user.prospects.create(params[:prospect])
respond_to do |format|
if #prospect.save
flash[:notice] = 'Prospect was successfully created.'
format.html { redirect_to(#prospect) }
format.xml { render :xml => #prospect, :status => :created, :location => #prospect }
else
format.html { render :action => "new" }
format.xml { render :xml => #prospect.errors, :status => :unprocessable_entity }
end
end
end
# PUT /prospects/1
# PUT /prospects/1.xml
def update
#prospect = current_user.prospects.find(params[:id])
respond_to do |format|
if #prospect.update_attributes(params[:prospect])
flash[:notice] = 'Prospect was successfully updated.'
format.html { redirect_to(#prospect) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #prospect.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /prospects/1
# DELETE /prospects/1.xml
def destroy
#prospect = Prospect.find(params[:id])
#prospect.destroy
respond_to do |format|
format.html { redirect_to(prospects_url) }
end
end
end
There's always Ryan Bate's Railscast on that subject. He's using the standard Rails autocomplete.
However, I prefer to use jQuery. I've used Dylan Verheul's autocomplete recently and found it very easy to set up.
try jquery autocomplete. i don't know anything about rails, but what you need to return to autocomplete should be easy enough even for a beginner.

Resources