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.
Related
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
Newbie to RoR here. I have built models with no namespace. One of them is called 'Brand'. I then proceeded to use rails g "admin/brands" to put maintenance functionality under an admin namespace, using rails generate scaffolding_controller "admin/brand" - which produced the views and the controller. The unit tests fail when I rake test:
NoMethodError: undefined method `admin_brands' for #<Admin::BrandsControllerTest:0x1034c0730>
test/functional/admin/brands_controller_test.rb:5:in `_callback_before_193'
in routes.rb I have:
# Administration routes
namespace :admin do
resources :brands
end
The generated controller code is as follows:
class Admin::BrandsController < ApplicationController
# GET /admin/brands
# GET /admin/brands.json
def index
#admin_brands = Brand.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #admin_brands }
end
end
# GET /admin/brands/1
# GET /admin/brands/1.json
def show
#admin_brand = Brand.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => #admin_brand }
end
end
# GET /admin/brands/new
# GET /admin/brands/new.json
def new
#admin_brand = Brand.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #admin_brand }
end
end
# GET /admin/brands/1/edit
def edit
#admin_brand = Brand.find(params[:id])
end
# POST /admin/brands
# POST /admin/brands.json
def create
#admin_brand = Brand.new(params[:admin_brand])
respond_to do |format|
if #admin_brand.save
format.html { redirect_to #admin_brand, :notice => 'Brand was successfully created.' }
format.json { render :json => #admin_brand, :status => :created, :location => #admin_brand }
else
format.html { render :action => "new" }
format.json { render :json => #admin_brand.errors, :status => :unprocessable_entity }
end
end
end
# PUT /admin/brands/1
# PUT /admin/brands/1.json
def update
#admin_brand = Brand.find(params[:id])
respond_to do |format|
if #admin_brand.update_attributes(params[:admin_brand])
format.html { redirect_to #admin_brand, :notice => 'Brand was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => #admin_brand.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /admin/brands/1
# DELETE /admin/brands/1.json
def destroy
#admin_brand = Brand.find(params[:id])
#admin_brand.destroy
respond_to do |format|
format.html { redirect_to admin_brands_url }
format.json { head :no_content }
end
end
end
Not sure how to debug this type of issue... I gather that paths are messed up somehow, but that is much as I can fathom at this point. Help appreciated.
In our company we dont use scaffold, especially when we need to generate admin namespace.
You can simply write admin namespace yourself.
config/routes.rb
namespace :admin do
root :to => "base#index"
resources :pages
# resources :states do
# member do
# get :make_default
# end
# end
end
app/controllers/admin/base_controller.rb
class Admin::BaseController < ApplicationController
before_filter :authenticate_user!, :admin_user?
layout "admin/admin"
def index
#page = Page.all
end
private
def admin_user?
redirect_to root_path, :alert => 'This page is allowed for admin' unless current_user.admin
end
end
app/views/admin/base/index.html.haml
= link_to "New Post", new_admin_post_path
%ul
- #post.each do |post|
%li= post.title
I am working on a project and im very new to rails,
I can't figure out what is wrong exectly.
I get this error.
NoMethodError in Products#index
uninitialized constant ProductsController::Offer
Esentially I have a feature im trying to implement.
in my products table I have a column called reserve price, I want a userto submit a number on the form on the products page which then validates it againts the reserve price, if accepted it gets added to cart , if not flash please enter higher offer,
the problem is I just can't seem to figure out how to get the model and controllers to work in tandem.
Iv'e been at this all week and I still don't have a clue.
I wondered if anyone could look at my code and see what im missing as for the view page I am getting the error that the undefined method `model_name' for NilClass:Class and I was sure I inputed the right model for the form, if I can get that working I can get the rest done quick but I dunno what im missing.
offer controller.rb
class OffersController < ApplicationController
attr_accessible :product, :offer , :reserve_price
def new
#offer = Offer.new
end
end
offer model.rb
class Offer < ActiveRecord::Base
belongs_to :product
has_many :reserve_prices
attr_accessible :product, :offer , :reserve_price
validates_presence_of :offer
validate :ensure_meets_reserve_price
private
def ensure_meets_reserve_price
if amount < self.product.reserve_price
errors.add(:amount, "does not meet reserve price")
end
end
private
def reserve_price
product.reserve_price
end
def your_offer
#your_offer = Offer.new
end
def new
#offer = Offer.new = :your_offer
end
end
Products index view file
class ProductsController < ApplicationController
before_filter :authenticate, :except => [:index, :show]
# GET /products
# GET /products.xml
def index
#offer = Offer.new
#products = Product.search(params[:search_query])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #products }
end
end
# GET /products/1
# GET /products/1.xml
def show
#product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #product }
end
end
# GET /products/new
# GET /products/new.xml
def new
#product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #product }
end
end
# GET /products/1/edit
def edit
#product = Product.find(params[:id])
end
# POST /products
# POST /products.xml
def create
#product = current_user.products.new(params[:product])
respond_to do |format|
if #product.save
format.html { redirect_to(#product, :notice => 'Product was successfully created.') }
format.xml { render :xml => #product, :status => :created, :location => #product }
else
format.html { render :action => "new" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.xml
def update
#product = Product.find(params[:id])
respond_to do |format|
if #product.update_attributes(params[:product])
format.html { redirect_to(#product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.xml
def destroy
#product = Product.find(params[:id])
#product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
end
Products controller.rb
class ProductsController < ApplicationController
before_filter :authenticate, :except => [:index, :show]
# GET /products
# GET /products.xml
def index
#products = Product.search(params[:search_query])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #products }
end
end
# GET /products/1
# GET /products/1.xml
def show
#product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #product }
end
end
# GET /products/new
# GET /products/new.xml
def new
#product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #product }
end
end
# GET /products/1/edit
def edit
#product = Product.find(params[:id])
end
# POST /products
# POST /products.xml
def create
#product = current_user.products.new(params[:product])
respond_to do |format|
if #product.save
format.html { redirect_to(#product, :notice => 'Product was successfully created.') }
format.xml { render :xml => #product, :status => :created, :location => #product }
else
format.html { render :action => "new" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.xml
def update
#product = Product.find(params[:id])
respond_to do |format|
if #product.update_attributes(params[:product])
format.html { redirect_to(#product, :notice => 'Product was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #product.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.xml
def destroy
#product = Product.find(params[:id])
#product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
end
any help?
much appricated ive been at this a while and have not figured it out!
If I understand your question correctly:
the error shows when accessing products#show
you want to include an offer form in the product#show page
In that case, you need to initialize the #offer variable in the ProductsController show action like so:
#offer = Offer.new
ADDITION
to next Problem: ProductsController::Offer is unknown, which it should not be as you have a Offer model. I've just tried including your Offer form into a show action, and it rendered it ok, apart from that you initialize the field with a new instance of Offer. (maybe an amount rather?). Anyway, I can't see from your code snippets why the Offer model is not available in your controller. can you provide the complete source?
I first suspected your strange private methods in Offer
def your_offer
#your_offer = Offer.new
end
def new
#offer = Offer.new = :your_offer
end
to be the cause, but I've included them and the form renders fine. But I what the heck are they supposed to do?
Having gone through my code I have a separate problem from my original question and rather than writing a new question. I will leave the old part at the bottom of this and post the new problem here. I do this because they are closely related.
New:
Im getting an error message saying
Unknown action
The action 'response' could not be found for CrawlerController
I'll keep it simple but the code for model, controller and routes are below in the previous question.
A basic run down is response is a def within CrawlerController as is add_Request.
The routes are matched as such:
match "/requests/new" => "crawler#add_Request"
match 'requests/:id' => 'crawler#response'
Here is controller code as per user request:
class CrawlerController < ApplicationController
def add_Request
#request = Request.new(params[:request])
respond_to do |format|
if #request.save
format.html { redirect_to(#request, :notice => 'Request was successfully created.') }
format.xml { render :xml => #request, :status => :created, :location => #request }
else
format.html { render :action => "new" }
format.xml { render :xml => #request.errors, :status => :unprocessable_entity }
end
end
end
def response
#request = Request.find(params[:id])
respond_to do |format|
format.html
format.js { render :json => #request }
end
end
def show
#request = Request.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #request }
format.json{
render :json => #request.to_json
}
end
end
end
please recheck code of controller as I can see it
class CrawlerController < ApplicationController
def add_Request
#request = Request.new(params[:request])
respond_to do |format|
if #request.save
format.html { redirect_to(#request, :notice => 'Request was successfully created.') }
format.xml { render :xml => #request, :status => :created, :location => #request }
else
format.html { render :action => "new" }
format.xml { render :xml => #request.errors, :status => :unprocessable_entity }
end
end
def response
#request = Request.find(params[:id])
respond_to do |format|
format.json {render :#request.to_json}
end
end
so one end is missing an your response action is defined inside add_Request
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