我正在使用脚手架实现一个简单的博客应用。有一个Post对象和一个Category对象。当我创建一个帖子" new"表单已提交,我还创建了一个Category对象并将其分配给帖子。但是由于某些原因,Category是一个nilClass对象。
_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
答案 0 :(得分:0)
根据this
在表单中尝试使用此功能...
<%= f.fields_for :category , @post.category do |category_form| %>
<div class="category">
<%= category_form.label :name %><br>
<%= category_form.text_area :name %>
</div>
<% end %>
...