私はdecent_exposure gemを使用するPostControllerを持っています。
#posts_controller.rb
class PostsController < ApplicationController
expose(:posts) { Post.all }
expose(:post) do
Post.find(params[:id]) || Post.new
end
def create
post = current_user.posts.new(params[:post])
if post.save
redirect_to post, notice: 'Post was successfully created.'
else
render action: :new
end
end
def update
if post.update_attributes(params[:post])
redirect_to post, notice: 'Post was successfully updated.'
else
render action: "edit"
end
end
def destroy
post.destroy
redirect_to posts_url, notice: "Post was deleted."
end
end
モデル投稿。
class Post < ActiveRecord::Base
extend FriendlyId
friendly_id :title, use: :slugged
acts_as_taggable_on :tags
belongs_to :user
has_many :comments, :dependent => :destroy
attr_accessible :title, :content, :tag_list
validates :title, :content, :user_id, :presence => true
validates :tag_list, :length => {:minimum => 1 ,:maximum => 6 }
end
そして _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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
#Fields.
<div class="actions">
<%= f.submit %>
</div>
しかし、新しい投稿を作成しようとするとエラーが発生するのはなぜですか?
Couldn't find Post without an ID
なんで?