如何在ror版本4中解决NoMethodError

时间:2015-01-12 09:29:12

标签: ruby-on-rails ruby ruby-on-rails-4

当我点击“显示您的数据”链接时,它显示以下错误。

错误:

NoMethodError in Users#show
Showing C:/Site/new/app/views/users/show.html.erb where line #10 raised:

undefined method `email' for #<User::ActiveRecord_Relation:0x2bd81e8>

我的代码片段如下所示。

视图/用户/ index.html.erb

<h1>Choose your option</h1>
<%= link_to "Enter your data",users_new_path %>
<%= link_to "Display your data",users_show_path %>

视图/用户/ new.html.erb

<h1>Enter your data here</h1>
<%= form_for @user,:url => {:action => 'create'} do |f| %>
    <% if @user.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@user.errors.count, "error") %> prohibited this post from being saved:</h2>

          <ul>
            <% @user.errors.full_messages.each do |message| %>
                <li><%= message %></li>
            <% end %>
          </ul>
        </div>
    <% end %>
    <%= f.text_field:name,placeholder:"Enter your name" %><br>
    <%= f.email_field:email,placeholder:"Enter your email" %><br>
    <%= f.text_field:message,placeholder:"Enter your message"%><br>
    <%= f.submit "Submit" %>
<% end %>

视图/用户/ show.html.erb

<h1>Display your data</h1>
    <ul>
      <li>
        <p>
          <strong>Name:</strong>
          <%= @user.name %>
        </p>
       <p>
         <strong>Email:</strong>
         <%= @user.email %>
       </p>
        <p>
          <strong>Message:</strong>
          <%= @user.message %>
        </p>
      </li>
    </ul>
<%= link_to "Edit",users_edit_path(:id => t.id) %><%= link_to "Delete",users_delete_path(:id => t.id),data: { confirm: 'Are you sure to delete it ?' }  %>
<%= link_to "Back",users_index_path %>

控制器/ users_controller.rb

class UsersController < ApplicationController
  def index

  end
  def new
    @user=User.new
  end
  def create
    @user=User.new(users_params)
    if @user.save
      flash[:notice]="Your data is saved succesfully"
      flash[:color]="valid"
      redirect_to :action => 'index'
    else
      flash[:alert]="You are entering wrong data"
      flash[:color]="invalid"
      render :new
    end
  end
  def show
    @user=User.all
  end
  def delete
   @user=User.find(params[:id])
    @user.delete
  end
  def edit
    @edit=User.find(params[:id])
    @user=User.new
  end
  def update
    @user=User.find(params[:id])
    if @user.update_attributes(update_params)
      flash[:notice]="Your data has updated successfully"
      flash[:color]="valid"
      redirect_to :action => 'index'
    else
      flash[:alert]="Your data could not update..check it.."
      flash[:color]="invalid"
      render :edit

    end
  end
  private
  def users_params
    params.require(:user).permit(:name, :email, :message,pets_attributes: [:name, :email,:message])
  end
  def update_params
    params.require (:user).permit(:name,:email,:message,pets_attributes: [:name,:email,:message])
  end
end

模型/ user.rb

class User < ActiveRecord::Base
  has_many :pets
  accepts_nested_attributes_for :pets
  EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
  validates :name,  :presence => true,:length => { :minimum => 5 }
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :message,  :presence => true
end

4 个答案:

答案 0 :(得分:2)

您应该修改show行动:

def show
  @user = User.all
end

为:

def show
  @user = User.find(params[:id])
end

答案 1 :(得分:1)

show行动中,您有

@user = User.all

将所有用户的关系分配给@user变量。这就是您遇到此错误的原因,因为您无法查询email的整个关系。你应该:

@user = User.find(params[:id])

User.find(params[:id])会返回单个User个实例。

答案 2 :(得分:0)

def show
    @user=User.all
  end

这将返回用户数组而不是用户,因此为了在其上调用电子邮件,您必须对其进行迭代循环。

<% user in @users %> <!-- I have change @user to @users in show method -->
  <h1>Display your data</h1>
    <ul>
      <li>
        <p>
          <strong>Name:</strong>
          <%= user.name %>
        </p>
       <p>
         <strong>Email:</strong>
         <%= user.email %>
       </p>
        <p>
          <strong>Message:</strong>
          <%= user.message %>
        </p>
      </li>
    </ul>
<%= link_to "Edit",users_edit_path(:id => t.id) %><%= link_to "Delete",users_delete_path(:id => t.id),data: { confirm: 'Are you sure to delete it ?' }  %>
<%= link_to "Back",users_index_path %>
<% end %>

或者只是更改show method

@user = User.find(params[:id])

答案 3 :(得分:-1)

修复你的节目动作。 更换 @user = User.all

@user = User.find(params[:id])