ror关联|未定义的方法“map”nil:nilclass

yrefmtwq  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(345)

当我想发布我的帖子时,我有这样一条:看到问题了吗
我在Linux上使用rails。
这是my 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

这种关系是多种多样的。我想用简单的形式与协会,但我不知道它如何正常工作。
提前谢谢你的放纵,我是法国人,英语不好。

9rnv2umw

9rnv2umw1#

如果无法保存@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

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

相关问题