rails 3 validations not allowing form submission - ruby-on-rails

I have a problem with validations in rails 3. I have got a model where i am simply trying to validate one of the field of the model form, that is that it should not be blank. But the problem is even though I have filled that text field still it shows the error. What could be the possible problems?
MODEL FILE:
class Page < ActiveRecord::Base
validates :title, :presence => true
end
_form.html.erb
<%= form_for(#page) do |f| %>
<% if #page.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#page.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #page.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.label :author %><br />
<%= f.text_field :author %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :reference %><br />
<%= f.select(:reference,[['google',1],['yahoo',2],['MSN',3],['Ask',4]]) %>
</p>
<%= f.submit "Submit" %>
<% end %>
And here is my controller file:
class PagesController < ApplicationController
def index
#total = Page.count
#pages = Page.find(:all)
end
def new
#page = Page.new
end
def create
#page = Page.new(params[:pages])
if #page.save
redirect_to pages_path, :notice => "The data has been saved!"
else
render "new"
end
end
def edit
#page = Page.find(params[:id])
if request.post?
#page.title = params[:title]
#page.author = params[:author]
#page.email = params[:email]
#page.body = params[:body]
#page.reference = params[:reference]
#page.save
redirect_to :action => 'index'
end
end
def destroy
#page = Page.find(params[:id])
#page.destroy
redirect_to :action => 'index'
end
end
My routes.rb is as follows:
Rorapp::Application.routes.draw do
get "home/index"
post "home/search"
get "home/_help"
get "home/create"
get "pages/index"
get "pages/new"
post "pages/new"
post "pages/edit"
get "pages/edit"
resources :pages
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'home#index'
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id(.:format)))'
end

You have duplicate routes.
Try replacing your routes.rb file with the following:
routes.rb
Rorapp::Application.routes.draw do
get "home/index"
post "home/search"
get "home/_help"
get "home/create"
resources :pages
root :to => 'home#index'
match ':controller(/:action(/:id(.:format)))'
end
Also you could create the update method in your PagesController, so your CRUD will be complete.

The create method in your controller refers to param[:pages]. Should this be param[:page]?

Related

Rails 4 Params not passed to the controller from the form

I am using a form for "GET" method. I created a text field (in testing.html.erb) for user to enter the name. I want the name to be passed into the controller and based on the name, I want to retrieve his data from the database through a query.
The problem here is that I am not getting anything into the instance variable in the controller action (Nothing is printed on screen when I display #testing in "testing.html.erb"). Below is my routes.rb file.
Rails.application.routes.draw do
resources :users
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
get 'index' => 'users#index'
get 'testing' => 'users#testing'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
.....
This is my testing.html.erb file
<h1> Testing page </h1>
<%= form_for users_path, :url => {:action => "testing", :name => :name}, :html => {:method => :get} do |f| %>
<%= f.label :first_name %><br />
<%= f.text_field :name %><br />
<%= f.submit "View Schedule" %>
<% end %>
<%= #testing %>
<%#= #testing.email %>
<%#= #testing.content %>
Please note that I commented #testing.email/content in above file to supress the error (undefined method `email' for nil:NilClass).
Below is my users_controller.rb file.
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
#before_action :user_set, only: [:testing]
# GET /users
# GET /users.json
def index
#users = User.all
#test = params[:name]
#test_index = params[:age]
end
# GET /users/1
# GET /users/1.json
def show
end
def testing
#testing = User.find_by(name: params[:name])
#if #testing.name == "Siri"
# #render text: "Hello #{#testing.name}"
#redirect_to action: :index
#end
end
.........
private
# Use callbacks to share common setup or constraints between actions.
def set_user
#user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email, :content)
end
end
The log-file shows the following.
Processing by UsersController#testing as HTML
Parameters: {"utf8"=>"✓", "/users"=>{"name"=>"hello rails"}, "commit"=>"View Schedule"}
I also tried to use strong params as User.find_by(name: params[:users][:name]) which throws error "undefined method `[]' for nil:NilClass".
I think I am going wrong somewhere. Please correct me. Thank you for your time.
Issue lies here:
Parameters: {"utf8"=>"✓", "/users"=>{"name"=>"hello rails"}, "commit"=>"View Schedule"}
Do you see /users key in your params? It shoudn't be there. This indicates problem with your form:
<%= form_for users_path, :url => {:action => "testing", :name => :name}, :html => {:method => :get} do |f| %>
First argument is expected to be a string (which is then used as a name of form params) or ActiveModel object. In your case, it is string returned by users_path, which is just '/users'. It should be #testing
<%= form_for #testing, :url => {:action => "testing", :name => :name}, :html => {:method => :get} do |f| %>
That will fix your current issue, you will get another shortly after that, which should go into a separate question.
You'll be best using the following:
#app/views/users/testing.html.erb
<%= #user.try(:name) %>
<%= form_tag users_testing_path, method: :get do |f| %>
<%= f.text_field :name %>
<%= f.submit "View Schedule" %>
<% end %>
This will allow:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def testing
#user = User.find_by name: params[:name]
end
end
You could improve your routes file too:
#config/routes.rb
resources :users, path: "", only: :index, path_names: { index: "index" } do #-> url.com/index
get :testing, on: :collection #-> url.com/testing
end

Why do I get NoMethodError in Games#new for my game form but the analog for my Users#new

I am creating a a site in RoR and and I have built the user signup and login forms. Everything works great. The thing is, I went to create another object called games, which functions almost identically to users, but when I try to interact with it I get an error. I built the forms almost exactly the same and the routing I congruent.
Here is my user new.html.erb:
<!DOCTYPE html>
<html>
<body>
<% provide(:title, 'Sign up') %>
<h1 class="heading1" >Sign up</h1>
<br>
<div>
<%= form_for(#user, :html => { :class => 'form' }) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<br>
<%= f.label :email %>
<%= f.text_field :email %>
<br>
<%= f.label :username %>
<%= f.text_field :username %>
<br>
<%= f.label :password %>
<%= f.password_field :password %>
<br>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<br>
<br>
<%= f.submit "Create my account", class: "submit" %>
<% end %>
</div>
</div>
</body>
</html>
and my users controller new and create methods:
def create
#user = User.new(user_params)
if #user.save
sign_in #user
redirect_to #user
else
render 'new'
end
end
def new
#user =User.new
end
private
def user_params
params.require(:user).permit(:name, :email, :username, :password,
:password_confirmation)
end
end
and my game new.html.erb:
<!DOCTYPE html>
<html>
<body>
<h1 class="heading1" >Create Game</h1>
<br>
<div>
<%= form_for(#game, :html => { :class => 'form' }) do |i| %>
<%= i.label :title %>
<%= i.text_field :title %>
<br>
<br>
<%= i.submit "Create Game", class: "submit" %>
<% end %>
</div>
</div>
</body>
</html>
and my game controller:
def create
#game = Game.new(game_params)
if #game.save
redirect_to root_url
else
render 'create'
end
end
def new
#game = Game.new
end
private
def game_params
params.require(:game).permit(:title)
end
end
and my routing file:
Rails.application.routes.draw do
resources :sessions, only: [:new, :create, :destroy]
resources :users
match '/new_game', to: 'games#new', via: 'get'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'home#home'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
The rails server error page reads:
NoMethodError in Games#new
Showing /Users/Karen/Desktop/BR2/app/views/games/new.html.erb where line #7 raised:
undefined method `games_path' for #<#<Class:0x007fbfd6bdb260>:0x007fbfd6bd8948>
Extracted source (around line #7):
4
5
6
7
8
9
10
<h1 class="heading1" >Create Game</h1>
<br>
<div>
<%= form_for(#game, :html => { :class => 'form' }) do |i| %>
<%= i.label :title %>
<%= i.text_field :title %>
<br>
Rails.root: /Users/Karen/Desktop/BR2
Application Trace | Framework Trace | Full Trace
app/views/games/new.html.erb:7:in `_app_views_games_new_html_erb___3427370169918602482_70230959128880'
Request
Parameters:
None
I really appreciate all and any help. if there is any more information I can provide please say so.
Thank you
The best option is to add games specific RESTful routes in routes.rb
resources :games
and remove match '/new_game', to: 'games#new', via: 'get' route.
Doing this will give you the following Restful routes:
Prefix Verb URI Pattern Controller#Action
games GET /games(.:format) games#index
POST /games(.:format) games#create
new_game GET /games/new(.:format) games#new
edit_game GET /games/:id/edit(.:format) games#edit
game GET /games/:id(.:format) games#show
PATCH /games/:id(.:format) games#update
PUT /games/:id(.:format) games#update
DELETE /games/:id(.:format) games#destroy
So upon form submission your application would route to create action (games_path) by HTTP Post request.
Currently you have just defined a single route for games resource which routes to new action with
match '/new_game', to: 'games#new', via: 'get'
But there is no route for create action which is why you receive the error as undefined method 'games_path' on the form
If you don't wish to use the RESTful routes(resources :games) then you would have to define a route as:
match '/games', as: 'games', to: 'games#create', via: 'post'
for create action.
You will find that when you do bundle exec rake routes in your console, you have not actually created named routes for your games paths.
If you're using match and you want to name a route (so you have something like games_path available), you'd have to do this:
match `/games`, as: 'games', to: 'games#index', via: :get
A much easier way is to use resources for most of your routes, and just go with the default RESTFUL paths:
resources :games
# Now you have access to '/games/new', '/games/:id',
# '/games', etc, as well as names such as `games_path`.
# Check `bundle exec rake routes` for all of them.
See Rails Routing for more information

Rails 3.2 error in controller: "Couldn't find Category with id="

First of all I would like to say Im very new on rails, so Im a noob.
I'm getting this error when I try to acess the "new forum page" from an app(forum monster) root/forums/new:
ActiveRecord::RecordNotFound in ForumsController#new
Couldn't find Category with id=
Here is my user model:
class User < ActiveRecord::Base
include Gravtastic
gravtastic :size => 165, :filetype => :png, :rating => 'R'
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
validates :email, :username, :presence => true, :uniqueness => true
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :username, :email, :password, :password_confirmation, :remember_me
has_many :topics, :dependent => :destroy
has_many :posts, :dependent => :destroy
def admin?
true if self.username == 'admin'
end
end
Here is my routes:
Community::Application.routes.draw do
resources :categories, :except => [:index, :show]
resources :forums, :except => :index do
resources :topics, :shallow => true, :except => :index do
resources :posts, :shallow => true, :except => [:index, :show]
end
root :to => 'categories#index', :via => :get
end
devise_for :users
root :to => 'categories#index', :via => :get
resources :users
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id(.:format)))'
end
and here is my view file:
<div class="module">
<div class="module_header"><%= action_name.humanize %> Forum</div>
<div class="module_subheader smaller">
<em>To create a category, leave the category field unselected.</em>
</div>
<div class="module_body">
<%= form_for #forum do |f| %>
<% if #forum.errors.any? %>
<% flash.now[:error] = #forum.errors.full_messages.join(', and ') %>
<% end %>
<div class="fieldset">
<span class="label indent smaller">
<%= f.label :category_id %><br />
<small>(Required)</small>
</span>
<span class="input indent smaller">
<%= f.collection_select :category_id, Category.all, :id, :title %>
</span>
<div class="clear"></div>
</div>
<div class="fieldset">
<span class="label indent smaller">
<%= f.label :title %><br />
<small>(Required)</small>
</span>
<span class="input indent smaller"><%= f.text_field :title, :size => 75 %></span>
<div class="clear"></div>
</div>
<div class="fieldset">
<span class="label indent smaller">
<%= f.label :description %><br />
<small>(Required)</small>
</span>
<span class="input indent smaller"><%= f.text_area :description, :cols => 60, :rows => 5 %></span>
<div class="clear"></div>
</div>
<div class="fieldset">
<span class="label indent smaller">
<%= f.label :position %>
</span>
<span class="input indent smaller"><%= f.text_field :position %></span>
<div class="clear"></div>
</div>
<div class="fieldset">
<span class="label indent smaller"></span>
<span class="input indent smaller">
<%= f.check_box :state %>
<%= f.label :state %>
</span>
<div class="clear"></div>
</div>
</div>
<div class="module_footer">
<div class="fieldset">
<span class="input"><%= f.submit "submit" %> or <%= link_to "cancel", #forum.nil? ? forum_path(#forum) : forums_path %></span>
<div class="clear"></div>
</div>
</div>
<% end %>
</div>
</div>
EDIT:
Here is my Forum controller:
class ForumsController < ApplicationController
load_and_authorize_resource :category
load_and_authorize_resource :forum, :through => :category, :shallow => true
def create
if #forum.save
flash[:notice] = "Forum was successfully created."
redirect_to forums_url
else
render :action => 'new'
end
end
def update
if #forum.update_attributes(params[:forum])
flash[:notice] = "Forum was updated successfully."
redirect_to forum_url(#forum)
end
end
def destroy
if #forum.destroy
flash[:notice] = "Category was deleted."
redirect_to forums_url
end
end
end
and also here is my Categories controller:
class CategoriesController < ApplicationController
load_and_authorize_resource :category
def create
if #category.save
flash[:notice] = "Category was successfully created."
redirect_to forums_url
else
render :action => 'new'
end
end
def update
if #category.update_attributes(params[:category])
flash[:notice] = "Category was updated successfully."
redirect_to forums_url
end
end
def destroy
if #category.destroy
flash[:notice] = "Category was deleted."
redirect_to forums_url
end
end
end
EDIT2:
My gemfile:
source 'http://rubygems.org'
gem 'rails', '3.2.14'
gem 'sqlite3'
gem 'forum_monster', '~> 1.0.3'
gem 'devise'
gem 'cancan'
gem 'gravtastic', :git => 'https://github.com/chrislloyd/gravtastic.git'
gem 'bb-ruby'
group :development do
gem 'hirb'
gem 'heroku'
end
group :production do
gem 'unicorn'
end
Sorry for being noob, Im new on rails.
I don't quite have enough experience with CanCan to say this with as much confidence as I'd like to, but I think your load_and_authorize_resource is off in the ForumsController.
I think what you're doing would make sense if your resources were like this:
resources :categories do
resources :forums
...
However, without this nesting, the load_and_authorize_resource :category in the forums controller is looking to initialize #category with data that does not exist.
If my suspicions are right (and I may well be misinterpreting what's going on here), I think the fix should be easy - just reduce the load_and_authorize_resource statements at the head to a single load_and_authorize_resource statement with no arguments. I don't think there would be a problem with doing this, but I suppose that depends on the security behind #category.

Generating wrong URL in the form action using form_for in rails4

I am using rails 4.0.0. I am a newcomer to rails and learning rails watching video tutorials and stuck in a problem so need some help.
I have a controller called Subjects and following is the code
Subject Constoller
class SubjectsController < ApplicationController
before_action :set_subject, only: [:show, :edit, :update]
def index
#subjects = Subject.all
end
def list
#subjects = Subject.all
##subjects = Subject.order("subjects.position ASC")
end
def show
#subject = Subject.find(params[:id])
end
def new
#subject = Subject.new(:name => "default")
end
def create
#subject = Subject.new(subject_params)
if #subject.save
redirect_to(:action => 'list')
else
render('new')
end
end
def edit
end
def update
if #subject.update(subject_params)
redirect_to #subject
else
render("edit")
end
end
def delete
#subject = Subject.find(params[:id])
end
def destroy
Subject.find(params[:id]).destroy
redirect_to(:action => "list")
end
private
# Use callbacks to share common setup or constraints between actions.
def set_subject
#subject = Subject.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def subject_params
params.require(:subject).permit(:name, :position, :visible)
end
end
I created a view for the List Action
View for the List Action
<h1>List</h1>
<div class="subject list">
<h2>Subjects</h2>
<table class="listing" summary="Subject list">
<tr class="header">
<th>Position</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to 'Show', subject, :class => 'action show'%>
<%= link_to "Edit", edit_subject_path(subject), :class => 'action edit' %>
<%= link_to "Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete' %>
</td>
</tr>
<% end %>
</table>
</div>
I also have a view for delete and code is as follows
View for Delete Action
<h1>Delete</h1>
<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>
<div class="subject delete">
<h2>Delete Subject</h2>
<%= form_for(:subject, :url => {:action => "destroy", :id => #subject.id}) do |f|%>
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= #subject.name %></p>
<div class="form-buttons">
<%= submit_tag("Delete Subject") %>
</div>
<% end %>
</div>
Here is the configuration of my routes file
SimpleCms::Application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'demo#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
get 'demo/index'
get 'demo/hello'
get 'demo/other_hello'
#get 'subjects/index'
get 'subjects/list'
get 'subjects/delete'
get '/subjects/:id/destroy' => 'subjects#destroy'
resources :subjects
get 'subjects/show'
get 'subjects/new'
get 'subjects/:id' => 'subjects#show'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
#resources :subjects
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
So the main concept is as follows when i will call http://localhost:3000/subjects/list it will show all the subjects and there is also three button that is show, edit and delete and after clicking on the delete link it will go to the delete view and after clicking on the submit button of the form it will be deleting the particular item.
But the main problem is that the form_for in delete view is producing the url in the form action is /subjects/8 where 8 is the id of the subject to be deleted but the url should be /subjects/destroy?id=8 to delete the item but when id is passed along with the action in form_for it produces the wrong url but when only the action is passed it produces the url as /subjects/destroy.
I cannot figure out what is wrong so please help. Thanks to all in advance.
Try a link with delete method instead use a form.
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= #subject.name %></p>
<%= link_to 'Delete Subject', #subject, method: :delete %>
UPDATE
If you want to use a form for destroy action, try this :
<% form_for #subject, :html => {:method => :delete} do |form| %>
Try this:
<%= form_for(#subject, :url => {:action => "destroy") do |f|%>
Instead of this:
<%= form_for(:subject, :url => {:action => "destroy", :id => #subject.id}) do |f|%>
You don't need to mention the id because in form_for it takes id of the variable mentioned by default.

Virtual Attributes: No route matches {:controller=>"phone", :action=>"new"}

I have a controller "find_numbers", which I'm using to submit a form to the Twilio API. Before it submits though, I'd like to validate against two form fields, which aren't in the data model for this controller. The fields are :name, and :original_number
So, in my find_numbers model, I added attr_accessor :name, attr_accessor :originial number to run a validates command under it.
After doing that and submitting the form as invalid, I get the error :
Routing Error
No route matches {:controller=>"phone", :action=>"new"}
Try running rake routes for more information on available routes.
I'm not sure why it says there's no roots, but I'm not sure why it's accessing that anyways. I want it to POST to find_numbers
The find_numbers/new template
<%= form_tag("/find_numbers", :method => "post", :id => "new_user" ) do %>
<%= render 'shared/error_messages' %>
<%= label_tag(:name, "What Are You Tracking?") %>
<%= text_field_tag(:name) %>
<%= label_tag(:original_number, "Your Orginal Number") %>
<%= text_field_tag(:original_number) %>
<%= label_tag(:in_postal_code, "Near US postal code (e.g. 94117):") %>
<%= text_field_tag(:in_postal_code) %>
<%= label_tag(:near_number, "Near this other number (e.g. +4156562345)") %>
<%= text_field_tag(:near_number) %>
<%= label_tag(:contains, "Matching this pattern (e.g. 415***EPIC):") %>
<%= text_field_tag(:contains) %>
<%= submit_tag("Search", :class => "btn btn-large btn-primary") %>
<% end %>
here's my find_number model
class FindNumber < ActiveRecord::Base
attr_accessor :name
attr_accessor :original_number
validates :name, presence: true
validates :original_number, presence: true
end
Here's my Find_number controller
class FindNumbersController < ApplicationController
def new
#user = current_user
end
def create
#user = current_user
client = Twilio::REST::Client.new(#user.twilio_account_sid, #user.twilio_auth_token)
search_params = {}
%w[in_postal_code near_number contains].each do |p|
search_params[p] = params[p] unless params[p].nil? || params[p].empty?
end
local_numbers = client.account.available_phone_numbers.get('US').local
#numbers = local_numbers.list(search_params)
unless #numbers.empty?
render 'find_numbers/show'
else
flash.now[:error] = "Sorry, We Couldn't Find Any Numbers That Matched Your Search! Maybe Something Simpler?"
render 'find_numbers/new'
end
end
def show
end
end
Any thoughts on accomplishing this would be greatly appreciated!
Edit
Routes.rb file
Dct::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :phones, only: [:new, :create, :destroy]
resources :find_numbers, only: [:new, :create, :destroy]
match '/find_numbers', to: 'find_numbers#new'
match '/signup', to: 'users#new'
match '/login', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
root to: 'static_pages#home'
match '/product_demo', to: 'static_pages#product_demo'
match '/pricing', to: 'plans#index'
match '/contact', to: 'static_pages#contact'
Edit
Here is the server log, of what happened when I hit submit
http://stepanp.com/railserror.jpg
Also, here's the find_numbers/show view
From what you've posted, the only other thing that looks suspicious to me is that you presumably have a PhonesController (plural) since you've declared resources :phones, but the routing error seems to occur because it is looking for a PhoneController (singular).

Resources