Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我有一个未定义的方法'name';零:零级,但我以前工作过_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 我有一个未定义的方法'name';零:零级,但我以前工作过

Ruby on rails 我有一个未定义的方法'name';零:零级,但我以前工作过,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我有一个未定义的方法“name”,它过去是有效的。。。我不知道我搞砸了什么。。。我需要你的帮助,请 index.html.slim .container .row .col-xs-12 h1.text-gray Tutorials h4 Search by Title =form_tag tutos_path, :method => 'get' do =text_field_tag :search, params[:sea

我有一个未定义的方法“name”,它过去是有效的。。。我不知道我搞砸了什么。。。我需要你的帮助,请

index.html.slim

.container
  .row
    .col-xs-12
      h1.text-gray Tutorials 
      h4 Search by Title
      =form_tag tutos_path, :method => 'get' do 
        =text_field_tag :search, params[:search]
        =submit_tag "Search", class:'btn btn-default'

    .col-xs-12
      -if user_signed_in?
        = link_to "Create a tuto", new_tuto_path, class:"btn btn-success"


#tutos.transitions-enabled
  -@tutos.each do |tuto|
    .box.panel-default
      -if tuto.category.name == "Ruby"
        = link_to(image_tag("select/ruby.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Rails 4"
        = link_to(image_tag("select/rails4.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Rails 5"
        = link_to(image_tag("select/rails5.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Heroku"
        = link_to(image_tag("select/heroku.png"), tuto_path(tuto))
      -elsif tuto.category.name == "AWS-Amazon"
        = link_to(image_tag("select/aws.png"), tuto_path(tuto))


      h3 = link_to tuto.title, tuto_path(tuto), class:"title-link"     
      h6 
        | Created by:
        span<>
        = tuto.user.full_name
      br
      span.glyphicon.glyphicon-heart
      span<>
      = tuto.get_upvotes.size
      br
      br
-@tutos.each do |tuto|
  .box.panel-default
    -if tuto.category.try(:name) == "Ruby"
      = link_to(image_tag("select/ruby.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Rails 4"
      = link_to(image_tag("select/rails4.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Rails 5"
      = link_to(image_tag("select/rails5.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Heroku"
      = link_to(image_tag("select/heroku.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "AWS-Amazon"
      = link_to(image_tag("select/aws.png"), tuto_path(tuto))
-@tutos.each do |tuto|
  .box.panel-default
    = link_to image_tag(image_by_category(tuto.category.try(:name))), tuto_path(tuto)
tuto_model.rb

class TutosController < ApplicationController
  before_action :authenticate_user!, only: [:new, :create]
  before_action :set_tuto, only: [:show, :edit, :update, :destroy, :upvote]


  def index
    @tutos = Tuto.all.includes(:user && :category)
    @categories = Category.all
    keyword_search
  end

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


  def new
    @tuto = Tuto.new
  end

  def edit
  end

  def create

    @tuto = Tuto.new(tuto_params)
    @tuto.user_id = current_user.id

    respond_to do |format|
      if @tuto.save
        flash[:success] = "Test"
        format.html { redirect_to @tuto, notice: 'Tuto was successfully created.' }
        format.json { render :show, status: :created, location: @tuto }
      else
        format.html { render :new }
        format.json { render json: @tuto.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
    respond_to do |format|
      if @tuto.update(tuto_params)
        format.html { redirect_to @tuto, notice: 'Tuto was successfully updated.' }
        format.json { render :show, status: :ok, location: @tuto }
      else
        format.html { render :edit }
        format.json { render json: @tuto.errors, status: :unprocessable_entity }
      end
    end
  end


  def destroy
    @tuto.destroy
    respond_to do |format|
      format.html { redirect_to tutos_url, notice: 'Tuto was successfully destroyed.' }
      format.json { head :no_content }
    end
  end


  def upvote
    @tuto.upvote_by current_user
    redirect_to :back
  end


  def keyword_search
   @tutos = Tuto.search(params[:search])
  end

  private

    def set_tuto
      @tuto = Tuto.find(params[:id])
    end

    def tuto_params
      params.require(:tuto).permit(:title, :content, :id, :user_id, :category_id)
    end
end
class Tuto < ActiveRecord::Base
  acts_as_votable
  belongs_to :user
  belongs_to :category
  validates :category_id, presence: true


  def self.search(search)
    if search
      where(["title LIKE ?","%#{search}%"])
    else
      all
    end
  end
end
class Category < ActiveRecord::Base
  has_many :tutos
end
def image_by_category(name)
  images = {
    "Ruby" => "select/ruby.png",
    "Rails 4" => "select/rails4.png",
    "Rails 5" => "select/rails5.png",
    "Heroku" => "select/heroku.png",
    "AWS-Amazon" => "select/aws.png"
  }
  images[name]
end
Tuto.all.includes(:用户和:类别)

应该是

Tuto.all.includes(:user, :category)

其中一个类别是
nil
,这就是为什么您得到

nil:NilClass的未定义方法“name”

使用
try

index.html.slim

.container
  .row
    .col-xs-12
      h1.text-gray Tutorials 
      h4 Search by Title
      =form_tag tutos_path, :method => 'get' do 
        =text_field_tag :search, params[:search]
        =submit_tag "Search", class:'btn btn-default'

    .col-xs-12
      -if user_signed_in?
        = link_to "Create a tuto", new_tuto_path, class:"btn btn-success"


#tutos.transitions-enabled
  -@tutos.each do |tuto|
    .box.panel-default
      -if tuto.category.name == "Ruby"
        = link_to(image_tag("select/ruby.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Rails 4"
        = link_to(image_tag("select/rails4.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Rails 5"
        = link_to(image_tag("select/rails5.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Heroku"
        = link_to(image_tag("select/heroku.png"), tuto_path(tuto))
      -elsif tuto.category.name == "AWS-Amazon"
        = link_to(image_tag("select/aws.png"), tuto_path(tuto))


      h3 = link_to tuto.title, tuto_path(tuto), class:"title-link"     
      h6 
        | Created by:
        span<>
        = tuto.user.full_name
      br
      span.glyphicon.glyphicon-heart
      span<>
      = tuto.get_upvotes.size
      br
      br
-@tutos.each do |tuto|
  .box.panel-default
    -if tuto.category.try(:name) == "Ruby"
      = link_to(image_tag("select/ruby.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Rails 4"
      = link_to(image_tag("select/rails4.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Rails 5"
      = link_to(image_tag("select/rails5.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Heroku"
      = link_to(image_tag("select/heroku.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "AWS-Amazon"
      = link_to(image_tag("select/aws.png"), tuto_path(tuto))
-@tutos.each do |tuto|
  .box.panel-default
    = link_to image_tag(image_by_category(tuto.category.try(:name))), tuto_path(tuto)
我还建议在代码中使用helper方法

index.html.slim

.container
  .row
    .col-xs-12
      h1.text-gray Tutorials 
      h4 Search by Title
      =form_tag tutos_path, :method => 'get' do 
        =text_field_tag :search, params[:search]
        =submit_tag "Search", class:'btn btn-default'

    .col-xs-12
      -if user_signed_in?
        = link_to "Create a tuto", new_tuto_path, class:"btn btn-success"


#tutos.transitions-enabled
  -@tutos.each do |tuto|
    .box.panel-default
      -if tuto.category.name == "Ruby"
        = link_to(image_tag("select/ruby.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Rails 4"
        = link_to(image_tag("select/rails4.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Rails 5"
        = link_to(image_tag("select/rails5.png"), tuto_path(tuto))
      -elsif tuto.category.name == "Heroku"
        = link_to(image_tag("select/heroku.png"), tuto_path(tuto))
      -elsif tuto.category.name == "AWS-Amazon"
        = link_to(image_tag("select/aws.png"), tuto_path(tuto))


      h3 = link_to tuto.title, tuto_path(tuto), class:"title-link"     
      h6 
        | Created by:
        span<>
        = tuto.user.full_name
      br
      span.glyphicon.glyphicon-heart
      span<>
      = tuto.get_upvotes.size
      br
      br
-@tutos.each do |tuto|
  .box.panel-default
    -if tuto.category.try(:name) == "Ruby"
      = link_to(image_tag("select/ruby.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Rails 4"
      = link_to(image_tag("select/rails4.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Rails 5"
      = link_to(image_tag("select/rails5.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "Heroku"
      = link_to(image_tag("select/heroku.png"), tuto_path(tuto))
    -elsif tuto.category.try(:name) == "AWS-Amazon"
      = link_to(image_tag("select/aws.png"), tuto_path(tuto))
-@tutos.each do |tuto|
  .box.panel-default
    = link_to image_tag(image_by_category(tuto.category.try(:name))), tuto_path(tuto)
应用程序\u helper.rb

class TutosController < ApplicationController
  before_action :authenticate_user!, only: [:new, :create]
  before_action :set_tuto, only: [:show, :edit, :update, :destroy, :upvote]


  def index
    @tutos = Tuto.all.includes(:user && :category)
    @categories = Category.all
    keyword_search
  end

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


  def new
    @tuto = Tuto.new
  end

  def edit
  end

  def create

    @tuto = Tuto.new(tuto_params)
    @tuto.user_id = current_user.id

    respond_to do |format|
      if @tuto.save
        flash[:success] = "Test"
        format.html { redirect_to @tuto, notice: 'Tuto was successfully created.' }
        format.json { render :show, status: :created, location: @tuto }
      else
        format.html { render :new }
        format.json { render json: @tuto.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
    respond_to do |format|
      if @tuto.update(tuto_params)
        format.html { redirect_to @tuto, notice: 'Tuto was successfully updated.' }
        format.json { render :show, status: :ok, location: @tuto }
      else
        format.html { render :edit }
        format.json { render json: @tuto.errors, status: :unprocessable_entity }
      end
    end
  end


  def destroy
    @tuto.destroy
    respond_to do |format|
      format.html { redirect_to tutos_url, notice: 'Tuto was successfully destroyed.' }
      format.json { head :no_content }
    end
  end


  def upvote
    @tuto.upvote_by current_user
    redirect_to :back
  end


  def keyword_search
   @tutos = Tuto.search(params[:search])
  end

  private

    def set_tuto
      @tuto = Tuto.find(params[:id])
    end

    def tuto_params
      params.require(:tuto).permit(:title, :content, :id, :user_id, :category_id)
    end
end
class Tuto < ActiveRecord::Base
  acts_as_votable
  belongs_to :user
  belongs_to :category
  validates :category_id, presence: true


  def self.search(search)
    if search
      where(["title LIKE ?","%#{search}%"])
    else
      all
    end
  end
end
class Category < ActiveRecord::Base
  has_many :tutos
end
def image_by_category(name)
  images = {
    "Ruby" => "select/ruby.png",
    "Rails 4" => "select/rails4.png",
    "Rails 5" => "select/rails5.png",
    "Heroku" => "select/heroku.png",
    "AWS-Amazon" => "select/aws.png"
  }
  images[name]
end

在这个@tutos循环中,您的一个类别不存在或为nil,因此name方法不存在于nil中,并向您抛出一个错误,为此,您可以用两种方法解决它

1) 检查循环开始时是否存在类别,如果存在,则仅进入

-@tutos.each do |tuto|
    .box.panel-default
    -if tuto.category.present?
        -if tuto.category.name == "Ruby"
        = link_to(image_tag("select/ruby.png"), tuto_path(tuto))
        -elsif tuto.category.name == "Rails 4"
        = link_to(image_tag("select/rails4.png"), tuto_path(tuto))
        -elsif tuto.category.name == "Rails 5"
        = link_to(image_tag("select/rails5.png"), tuto_path(tuto))
        -elsif tuto.category.name == "Heroku"
        = link_to(image_tag("select/heroku.png"), tuto_path(tuto))
        -elsif tuto.category.name == "AWS-Amazon"
        = link_to(image_tag("select/aws.png"), tuto_path(tuto))
2) 使用
try
继续执行而不检查错误

-@tutos.each do |tuto|
    .box.panel-default
      -if tuto.try(:category).try(:name) == "Ruby"
        = link_to(image_tag("select/ruby.png"), tuto_path(tuto))
      -elsif tuto.try(:category).try(:name) == "Rails 4"
        = link_to(image_tag("select/rails4.png"), tuto_path(tuto))
      -elsif tuto.try(:category).try(:name) == "Rails 5"
        = link_to(image_tag("select/rails5.png"), tuto_path(tuto))
      -elsif tuto.try(:category).try(:name) == "Heroku"
        = link_to(image_tag("select/heroku.png"), tuto_path(tuto))
      -elsif tuto.try(:category).try(:name) == "AWS-Amazon"
        = link_to(image_tag("select/aws.png"), tuto_path(tuto))        

您的
@tuto
可能没有关联的类别。也许您可以显示stacktrace和
@tuto
的值?谢谢我的尝试,每行之间应该有comas吗?这给了我一个错误。。。def image_by_category(name)images={“Ruby”=>“select/Ruby.png”、“Rails 4”=>“select/rails4.png”、“Rails 5”=>“select/rails5.png”、“Heroku”=>“select/Heroku.png”、“AWS Amazon”=>“select/AWS.png”}images[名称]end@Johan:是的,应该有逗号:)@Deepak:而不是
try
,可以使用lonely运算符。@SergioTulentsev受ruby2.3及更高版本支持only@SergioTulentsev更新了答案缺少一个括号