Reputation: 3999
While creating a search form I am facing a problem. I am getting the following error:
undefined method `model_name' for NilClass:Class
This is my view file:
"datepicker" %>This is my clients_controller.rb:
class ClientsController < ApplicationController
def newClients
end
end
And this is my model client.rb:
class Client < ActiveRecord::Base
# attr_accessible :title, :body
end
I am confused in using form_for
parameter. Can any one explain it briefly how and why to use form_for
parameter?
Edit 1
I have modified my controller as
class ClientsController < ApplicationController
def search
redirect_to root_path
end
end
Once i click submit button it showing error as
No route matches [GET] "/search"
Upvotes: 0
Views: 53
Reputation: 17631
You are missing something here. Let me explain.
In your controller you don't need to define a custom method (called newClients
) since Rails conventions suggest to use the following:
class ClientsController < ApplicationController
# GET /clients
def index
@clients = Client.all
end
# GET /clients/:id
def show
@client = Client.find(params[:id])
end
# GET /clients/new
def new
@client = Client.new
end
# POST /clients
def create
@client = Client.new(params[:client])
if @client.save
redirect_to :back, success: "Successfully created..."
else
render :new
end
end
# GET /clients/:id/edit
def edit
@client = Client.find(params[:id])
end
# PUT /clients/:id
def update
@client = Client.find(params[:id])
if @client.update_attributes(params[:client])
redirect_to :back, success: "Successfully edited..."
else
render :edit
end
end
# DELETE /clients/:id
def destroy
@client = Client.find(params[:id]).destroy
redirect_to :back, success: "Successfully deleted..."
end
end
And finally, in order for your form_for
to work properly, you need to pass it an instance of a class:
form_for @client
where @client
is Client.new
in your case.
Upvotes: 2
Reputation: 12320
First of all in your controller please follow Rails naming conventions. The method name should be new_clients
or new
.
def new
@client = Client.new
end
Your view name should be new.html.erb.
You are not defining @client
in your controller, but in the view you are using it.
Upvotes: 0