客户端#new中的NoMethodError

时间:2014-04-06 18:22:05

标签: ruby-on-rails view model controller nomethoderror

所以我在我的一个视图中收到此错误,这似乎与客户端在控制器中初始化的方式有关,这里是我的客户端模型和控制器的代码:< / p>

型号:

class Client < ActiveRecord::Base
    has_many :visits
    has_many :exchanges, through: :visits

控制器:

class ClientsController < ApplicationController
    before_action :set_client, only: [:show, :edit, :update, :destroy]

    def index
        @clients = Client.all
    end


    # GET /clients/new
    def new
        @client = Client.new
    end

   # POST /clients
   # POST /clients.json
   def create
        @client = Client.new(client_params)

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

  # PATCH/PUT /clients/1
  # PATCH/PUT /clients/1.json
  def update
      respond_to do |format|
        if @client.update(client_params)
            format.html { redirect_to @client, notice: 'Client was successfully updated.' }
            format.json { head :no_content }
        else
            format.html { render action: 'edit' }
            format.json { render json: @client.errors, status: :unprocessable_entity }
        end
     end
  end

  # DELETE /clients/1
  # DELETE /clients/1.json
  def destroy
      @client.destroy
      respond_to do |format|
        format.html { redirect_to clients_url }
        format.json { head :no_content }
      end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_client
      @client = Client.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def client_params
      #hidden for security
  end
end

因此,当我调用@client时,错误在我的客户端表单的第1行出现,并说“未定义的方法`model_name&#39; for ActiveRecord :: Relation :: ActiveRecord_Relation_Client:Class&#34;真的可以使用一些帮助,因为我不知道发生了什么,谢谢。

错误堆栈:

Started GET "/clients/new" for 127.0.0.1 at 2014-04-06 14:05:48 -0400
Processing by ClientsController#new as HTML
Rendered clients/_form.html.erb (62.2ms)
Rendered clients/new.html.erb within layouts/application (65.3ms)
Completed 500 Internal Server Error in 68ms

ActionView::Template::Error (undefined method `model_name' for ActiveRecord::Relation::ActiveRecord_Relation_Client:Class):
1: <%= simple_form_for(@client) do |f| %>
2:   <% if @client.errors.any? %>
3:     <div id="error_explanation">
4:       <h2><%= pluralize(@client.errors.count, "error") %> prohibited this client from being saved:</h2>
app/views/clients/_form.html.erb:1:in `_app_views_clients__form_html_erb___2200324505640086965_70348410359440'
app/views/clients/new.html.erb:3:in `_app_views_clients_new_html_erb___3059029162300151807_70348376263300'


Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.6ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Library/Ruby/Gems/2.0.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (12.8ms)

0 个答案:

没有答案