ruby-on-rails 在@post.create上,我得到一个没有方法的错误--我无法跟踪为什么调用这个方法

ffdz8vbo  于 2023-03-24  发布在  Ruby
关注(0)|答案(1)|浏览(118)

我有一个post控制器,并不断得到一个由@ post. create触发的无方法错误。下面是错误消息,我无法追踪这是从哪里来的-nomethod错误没有给出任何线索。
错误:

undefined method `each' for "":String

控制器/错误源:posts_controller.rb

class PostsController < ApplicationController
  require 'securerandom'
  before_action :set_post, only: %i[ show edit update destroy ]
  
  # GET /posts or /posts.json
  def index
    @city = request.location.city
    @category = Category.find_by(title: params[:category_name])
    @posts = Post.where(section_id: params[:section_id], category_name:   params[:category_name], section_name: params[:section_name])
    if params[:street] != nil && params[:city] != nil && params[:postal_code] != nil && params[:country_code] != nil
      @posts = @posts.near(["%#{params[:street]}%","%#{params[:city]}%","%#{params[:country_code]}%"].compact.join(', '), :distance)
    end
    @hash = Gmaps4rails.build_markers(@posts) do |post, marker|
      marker.lat post.latitude
      marker.lng post.longitude
    end
  end
  
  # GET /posts/1 or /posts/1.json
  def show
  end
  
  # GET /posts/new
  def new
    @post = Post.new
    @post.uuid = SecureRandom.uuid
    @post.section_id = params[:section_id]
    @post.category_name = params[:category_name]
    @post.section_name = params[:section_name]
  end
  
  # GET /posts/1/edit
  def edit
  end
  # POST /posts or /posts.json
  def create
    @post = Post.create(post_params)
    @post.uuid = SecureRandom.uuid
    if params[:city] == nil || params[:country_code] == nil ||  params[:title] == nil
      flash[:notice] = "You must fill in a Title, City, and Country to save."
      redirect_to new_section_post_path(section_id: @post.section_id, category_name: @post.category_name, section_name: @post.section_name)
    else
      @post.geocode
      
      respond_to do |format|
        if @post.save
          format.html { redirect_to section_post_url(id: @post.id, section_id: @post.section_id), notice: "Post was successfully created." }
          format.json { render :show, status: :created, location: @post }
        else
          format.html { render :new, status: :unprocessable_entity }
          format.json { render json: @post.errors, status: :unprocessable_entity }
        end
      end
    end
  end
  
  # PATCH/PUT /posts/1 or /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to section_post_url(id: @post.id, section_id: @post.section_id), notice: "Post was successfully updated." }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end
  
  # DELETE /posts/1 or /posts/1.json
  def destroy
    @post.destroy
    
    respond_to do |format|
      format.html { redirect_to posts_url, notice: "Post was successfully destroyed." }
      format.json { head :no_content }
    end
  end
  
  private
  # Use callbacks to share common setup or constraints between actions.
  def set_post
    @post = Post.find(params[:id])
  end
  
  # Only allow a list of trusted parameters through.
  def post_params
    params.require(:post).permit(:title, :uuid, :latitude, :longitude, :category_name, :section_name, :city, :postal_code, :country_code, :flags, :section_id, :street, :content)
  end
end

错误日志:

Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.0ms | Allocations: 4178)


NoMethodError (undefined method `each' for "":String

    other_array.each { |val| raise_on_type_mismatch!(val) }
               ^^^^^):

 app/controllers/posts_controller.rb:38:in `create'

型号:

class Post < ApplicationRecord
belongs_to :section
has_many :flags
has_many :messages
geocoded_by :address
has_rich_text :content

def address
[street, city, country_code].compact.join(', ')
end
end

~
这就是我所有的信息,我只是想知道是否有人见过这个错误之前?它显然是activerecord,但我错过了什么是不清楚。我只输入字符串在迁移中,我输入的所有参数都是字符串,只有标志和section_id是整数。
model.create抛出了我无法跟踪的错误。

x7yiwoj4

x7yiwoj41#

在我看来,你试图添加一个与Post相关的Flag对象的集合,但你现在这样做的方式,你只是向你的控制器发送一个字符串。你可以很容易地在你的控制台中复制这个问题,比如Post.first.update(flags: "99"),你应该得到一个类似的错误。
flags应该是ActiveRecord对象的集合,只要它是与另一个模型的has_many关系,而不是posts表中名为flags的列。更新has_many关联的另一种方法是传入flag_ids的数组,因此这两个选项都应该有效:

Post.first.update(flags: Flag.first(3))
Post.first.update(flag_ids: [1, 2, 3])

所以,你应该发送一个ID数组而不是字符串到你的控制器。修改参数以接受一个值数组而不是像这样的单个值:

params.require(:post).permit(:title, :uuid, ..., flag_ids: [])

您还需要更新前端以将flag_ids作为数组发送。如果您想选择多个项目,collection_check_boxes通常是一个不错的选择。

<%= form.collection_check_boxes :flag_ids, Flag.all, :id, :name ... %>

相关问题