简体   繁体   English

尝试创建“标签”时未定义的方法“map”

[英]Undefined method 'map' when trying to create 'tags'

I'm repurposing this guide https://rubyplus.com/articles/4241-Tagging-from-Scratch-in-Rails-5 to allow me to have 'tastingsnotes' as a tag on a 'roast.rb' model. 我正在重新使用本指南https://rubyplus.com/articles/4241-Tagging-from-Scratch-in-Rails-5,以允许我将'tastingsnotes'作为'roast.rb'模型上的标签。

However, I'm getting the error undefined method 'map' when trying to show the record in the browser. 但是,当我试图在浏览器中显示记录时,我得到错误undefined method 'map' I'm fairly sure this is down to me not mapping the tags correctly somehow. 我很确定这不是我不能以某种方式正确映射标签。 I can see when I try to edit the record, I have the tags there. 我可以看到当我尝试编辑记录时,我在那里有标签。

The logic is that I have a model called 'Roasts' I want the user to be a able to add a comma-separated list of tasting notes. 逻辑是我有一个名为'Roasts'的模型我希望用户能够添加以逗号分隔的品尝笔记列表。 I'm therefore treating these as tags. 因此,我将这些视为标签。

roast.rb roast.rb

class Roast < ApplicationRecord
  has_many :tastings
  has_many :notes, through: :tastings

  def self.tagged_with(name)
    Note.find_by!(name: name).roasts
  end

  def self.note_counts
    Note.select('notes.*, count(tastings.note_id) as count').joins(:tastings).group('tastings.note_id')
  end

  def tastingnotes
    notes.map(&:name).join(', ')
  end

  def tastingnotes=(names)
    self.notes = names.split(',').map do |n|
      Note.where(name: n.strip).first_or_create!
    end
  end
end

note.rb note.rb

class Note < ApplicationRecord
  has_many :tastings
  has_many :roasts, through: :tastings
end

tasting.rb tasting.rb

class Tasting < ApplicationRecord
  belongs_to :note
  belongs_to :roast
end

roasts/_form.html.rb 烤肉/ _form.html.rb

//items not pasted for brevity
      <div class="form-group">
        <%= form.label :tastingnotes, "Notes (separated by commas)", class: 'control-label' %><br  />
        <%= form.text_area :tastingnotes, id: :roast_tastingnotes, class: "form-control" %>
      </div>
//items not pasted for brevity

roasts/show.html.rb 烤肉/ show.html.rb

//items not pasted for brevity
<p>
  <strong>Tasting Notes</strong>
  <%= raw @roast.tastingnotes.map(&:name).map { |t| link_to t, tastingnotes_path(t) }.join(', ') %>
</p>
//items not pasted for brevity

Console error: 控制台错误:

Completed 500 Internal Server Error in 18ms (ActiveRecord: 1.1ms)



ActionView::Template::Error (undefined method `map' for "chocolate, citrus":String
Did you mean?  tap):
    39:
    40: <p>
    41:   <strong>Tasting Notes</strong>
    42:   <%= raw @roast.tastingnotes.map(&:name).map { |t| link_to t, tastingnotes_path(t) }.join(', ') %>
    43: </p>
    44:
    45:

You did 你做到了

 def tastingnotes
    notes.map(&:name).join(', ')
  end

which return String of comma seperated like "choco, beer" 返回逗号分隔符串如“choco,beer”

Now you did map to a string in below 现在你确实映射到下面的字符串

<%= raw @roast.tastingnotes.map(&:name).map { |t| link_to t, tastingnotes_path(t) }.join(', ') %>

You can try 你可以试试

  def tastingnotes
    notes.pluck(:name)
  end

and

<%= raw @roast.tastingnotes.map { |t| link_to t, tastingnotes_path(t) } %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试创建新用户时未定义的方法“ with_scope” - undefined method `with_scope' when trying to create new user 尝试在控制器中创建过滤器时获取“未定义的方法” - Getting “Undefined method” when trying to create filter in a controller 尝试在用户模型中创建配置文件时,nil:NilClass的未定义方法“ create” - undefined method `create' for nil:NilClass when trying to create profile in user model 尝试创建评论。 (未定义的方法“注释”) - Trying to create comments. (Undefined method 'comment') NoMethodError(未定义的方法 `persist&#39; for #<PersonalTrainer:0x000055625dbfd7a8> 尝试创建表条目时 - NoMethodError (undefined method `persist' for #<PersonalTrainer:0x000055625dbfd7a8> when trying to create a table entry 尝试手动创建用户时出现“ undefined method`generate_token&#39;”错误 - “undefined method `generate_token'” error when trying to manually create a user 尝试在数据库中创建记录时,NilClass:Class的未定义方法“ model_name” - undefined method `model_name' for NilClass:Class when trying to create a record in my database 尝试访问相关记录时未定义的方法 - undefined method when trying to access related record 尝试访问密钥时出现未定义的方法 [] 错误 - Undefined method[] error when trying to access a key 尝试传递ID时未定义的方法 - Undefined method when trying to pass through id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM