简体   繁体   中英

undefined method `model_name' for NilClass:Class when trying to create a record in my database

Ive got a problem with my Rails app.

When I try to create a new client in my clients table im getting this error => undefined method model_name for NilClass:Class

It's telling me the error is in my show view at line #2.

1: <%- model_class = @client.class -%>
2: <h1><%=t '.title', :default => model_class.model_name.human %></h1>
3: 
4: <p>
5:   <strong><%= model_class.human_attribute_name(:name) %>:</strong><br>

Heres my show view..

<%- model_class = @client.class -%>
<h1><%=t '.title', :default => model_class.model_name.human %></h1>

<p>
  <strong><%= model_class.human_attribute_name(:name) %>:</strong><br>
  <%= @client.name %>
</p>
<p>
  <strong><%= model_class.human_attribute_name(:detail) %>:</strong><br>
  <%= @client.detail %>
</p>
<p>
  <strong><%= model_class.human_attribute_name(:more_detail) %>:</strong><br>
  <%= @client.more_detail %>
</p>
<p>
  <strong><%= model_class.human_attribute_name(:more_details) %>:</strong><br>
  <%= @client.more_details %>
</p>

<div class="form-actions">
  <%= link_to t('.back', :default => t("helpers.links.back")),
              clients_path, :class => 'btn'  %>
  <%= link_to t('.edit', :default => t("helpers.links.edit")),
              edit_client_path(@client), :class => 'btn' %>
  <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
              client_path(@client),
              :method => 'delete',
              :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
              :class => 'btn btn-danger' %>
</div>

Here's my controller =>

class ClientsController < ApplicationController

  respond_to :html def index @clients = Client.all respond_with (@clients) end def new @clients = Client.new respond_with (@clients) end def create @clients = Client.new(params[:name]) if @clients.save flash[:notice] = "Client successfully created" end respond_with(@clients) end def destroy @clients = Client.find(params[:id]) @clients.destroy flash[:notice] = "Client has been removed." respond_with(@clients) end end 

I can't see where the problem is?

Ive recently installed the rails-twitter-bootstrap gem but don't think that would effect it much.

You're setting in your controller the value of @clients (with s at the end) and accessing in your view just the @client variable, ie without s . Fix this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM