【问题标题】:ROR Association | undefined method `map' for nil:NilClassROR 协会 | nil:NilClass 的未定义方法“map”
【发布时间】:2019-01-31 07:31:36
【问题描述】:

当我想发布我的帖子时,我有这个: See the problem

我在 Archlinux 上使用 Rails。

这是我的控制器 posts_controller.rb:

module Admin
  class PostsController < ApplicationController
    before_action :set_post, :set_categories, only: [:update, :edit, :destroy]
    layout 'back'
    def index
      @posts = Post.all.order('created_at DESC')##.page(params[:page]).per(10)
    end

    def new
      @posts = Post.new
      @users = current_user
      @categories = Category.all.map{|c| [ c.name, c.id ] }
    end

    def create
      @posts = Post.new(post_params)
      @posts.category_id = params[:category_id]
      if @posts.save
        redirect_to({action: :index}, success: "L'article à bien été crée")
      else
        render :new
      end
    end

    def edit
      @categories = Category.all.map{|c| [ c.name, c.id ] }
    end

    def update
      if @posts.update(post_params)
        @posts.category_id = params[:category_id]
        redirect_to({action: :index}, success: "L'article à bien été modifié")
      else
        render :new
      end
    end

    def destroy
      @posts.destroy
      redirect_to({action: :index}, success: "L'article à bien été supprimé")
    end

    private

    def post_params
      params.require(:post).permit(:title, :meta, :description, :precontent, :content, :category_id)
    end

    def set_post
      @posts = Post.friendly.find(params[:id])
    end

    def set_categories
      @categories = Category.all
    end



  end
end

这是我的看法:

<div class="row">
  <div class="col-md-9">
    <div class="card">
      <div class="card-header">Ajouter un article</div>
      <div class="card-body">
        <%= simple_form_for @posts, url: [:admin, @posts], html:{class: 'form-horizontal'} do |f| %>
          <%= f.input :title, label: "Titre" %>
          <%= f.input :meta, label: "Meta TAGS (Référencement)" %>
          <%= f.input :description, label: "Description" %>
          <%= f.input :precontent, label: "Pré Contenu" %>
          <%= f.input :content, label: "Contenu" %>
          <button type="submit" class="btn btn-primary">Ajouter l'article</button>
      </div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card">
      <div class="card-header">Image & Catégories</div>
      <div class="card-body">
        <%= select_tag(:category_id, options_for_select(@categories), class: 'form-control') %>
        <% end %>
      </div>
    </div>
  </div>
</div>

这是我的模特帖子:

class Post < ApplicationRecord
  extend FriendlyId
  friendly_id :title, use: [:slugged]
  belongs_to :category
  belongs_to :user
end

这是我的模特类别:

class Category < ApplicationRecord
  has_many :posts
end

关系是ManyToOne。 我想使用简单的关联形式,但我不知道它是如何正常工作的。

提前感谢您的宽容,我是法国人,我的英语不好。

【问题讨论】:

  • 你能在这里添加整个错误转储吗.. 然后我们可以快速找出它繁荣从哪里出来?
  • 我觉得它失败了,当编辑/创建失败时......所以@categories也必须在创建和更新操作中定义

标签: mysql ruby-on-rails ruby associations


【解决方案1】:

如果无法保存@post,您必须在“create”操作上定义@categories(以及操作“new”所需的任何其他变量)。

if @post.save
  ....
else
   @categories = Category.select(:name,:id).map{|c| [c.name, c.id]} #note the select, it will be faster than just calling all
   render :new
end

对于“更新”操作也可以这样做,但我建议只重定向到 @post 的编辑路径而不是渲染 :new(您确定要渲染 :new 吗?)

【讨论】:

  • 当我输入你的线路时,我没有错误但我的帖子没有发布我在同一页面上,请参阅:plixup.com/pics_core3/…
  • @post 没有被保存,你必须检查是否有一些验证阻止了它。您应该通知用户验证错误,您也可以使用 byebug 或 pry-rails 实时调试应用程序
  • 我猜这个类别实际上并没有被设置,因为它在表格之外。检查您的日志,category_id 必须为空。只需将表单初始化移到所有字段之外或使用选择的表单属性从w3schools.com/tags/att_select_form.asp 之外指向表单
猜你喜欢
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多