Jazz
Jazz

Reputation: 1090

Ruby on Rails: undefined method, error with Controller methods

I'm making a website where the user can input data into a database, and then can search the database on another page. At the moment the create new entry form works fine, and so does the search page for displaying the tables, as I haven't implemented the search yet.

Controller:

def search
 @project_search = Project.order(sort_column + " " + sort_direction)
end

  private

   def sort_column
    Project.column_names.include?(params[:sort]) ? params[:sort] : "project_name"
   end

   def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
   end


def create
@project = Project.new(params[:project])


@project.client = params[:new_client] unless params[:new_client].blank?
@project.exception_pm = params[:new_exception_pm] unless params[:new_exception_pm].blank?
@project.project_owner = params[:new_project_owner] unless params[:new_project_owner].blank?
@project.role = params[:new_role] unless params[:new_role].blank?
@project.industry = params[:new_industry] unless params[:new_industry].blank?
@project.business_div = params[:new_business_div] unless params[:new_business_div].blank?

respond_to do |format|
  if @project.save
    format.html { redirect_to @project, notice: 'Project was successfully created.' }
    format.json { render json: @project, status: :created, location: @project }
  else
    format.html { render action: "new" }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
end
end

When I comment out the search method, my input form then works, but when they are both there, I get this error.

undefined method `model_name' for NilClass:Class

Extracted Source, from my form view at line 1:

<%= form_for(@project) do |f| %>

I think it's just a small problem, but I can't see how to fix it. I am new to ruby on rails, so go easy :).

Thanks!

Upvotes: 1

Views: 2294

Answers (1)

Peter Brown
Peter Brown

Reputation: 51697

Not 100% sure, but I think it's because of where private is located. In Ruby, everything after that call will be private. Rails may ignore your definition of create and use its own. Any chance you are commenting out private when you say you "comment out the search method"?

Upvotes: 2

Related Questions