mardi 8 septembre 2015

Implementing a simple Post/Category relationship with scaffold, Category is a nilClass

I'm implementing a simple blog app using scaffolding. There is a Post object and a Category object. When I create a Post once the "new" form is submitted, I also create a Category object and assign it to the post. However for some reason Category is a nilClass object.

_form.html.erb:

    <%= form_for(@post) do |f| %>

  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :content %><br>
    <%= f.text_area :content %>
  </div>
   <%= f.fields_for :category do |category_form| %>
     <div class="category">
        <%= category_form.label :name %><br>
        <%= category_form.text_area :name %>
    </div>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

categories_controller.rb

class CategoriesController < ApplicationController
def new
    @category = Category.new
end

def edit
    @category = Category.find(params[:id])
end

def create
  @category = Category.new(category_params)
  @category.save
end

def update
  @category = Category.find(params[:id])
end

def destroy
  @category = Category.find(params[:id])
  @category.destroy
end

private
  def category_params
    params.require(:category).permit(:name)
  end 
end

category.rb

class Category < ActiveRecord::Base
    has_many :posts
end

post.rb

class Post < ActiveRecord::Base
    belongs_to :category
    accepts_nested_attributes_for :category
end

posts_controller.rb

    class Post < ActiveRecord::Base
        belongs_to :category
        accepts_nested_attributes_for :category
    end

    class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)
    @category = Category.create(post_params[category_attributes:[:name]])
    @post.category = @category
    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /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

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :content, category_attributes: [:name])
    end
end

Aucun commentaire:

Enregistrer un commentaire