前提
rails で投稿アプリを作っています。
実現したいこと
エラーの修正
カテゴリー機能の実装
カテゴリー検索機能の実装
発生している問題・エラーメッセージ
NameError in Posts#new Showing C:/Users/user/Desktop/sentinel/app/views/posts/new.html.erb where line #19 raised: undefined local variable or method `form' for #<ActionView::Base:0x00000000024248> Did you mean? for fork
該当のソースコード
<h1>新規投稿</h1> <%= form_with model: @post, url: '/posts', local:true do |f| %> <div class="field"> <h4>タイトル</h4> <%= f.text_field :title %> <h4>あらすじ</h4> <%= f.text_area :summary %> <h4>見どころ</h4> <%= f.text_area :highlight %> </div> <div class="field"> <%= f.label :image %> <%= f.file_field :image %> </div> <div class="check_box"> <span>タグ</span> <%= form.collection_check_boxes(:tag_ids, Tag.all, :id, :name) do |tag| %> <div> <%= tag.label do %> <%= tag.check_box %> <%= tag.text %> <% end %> </div> <% end %> </div> <%= f.submit '投稿' %> <% end %>
def index @posts = params[:tag_id].present? ? Tag.find(params[:tag_id]).posts : Post.all @posts = @posts.where('title LIKE ?', "%#{params[:search]}%") if params[:search].present? end def new @post = Post.new end def create post = Post.new(post_params) post.user_id = current_user.id if post.save redirect_to post_path(post.id) else redirect_to :action => "new" end end def post_params params.require(:post).permit(:title, :summary, :highlight, :image, tag_ids: []) end def show @post = Post.find(params[:id]) @comments = @post.comments @comment = Comment.new end def edit @post = Post.find(params[:id]) end def update post = Post.find(params[:id]) post.update(post_params) redirect_to post_path(post.id) end def destroy post = Post.find(params[:id]) post.destroy redirect_to posts_path end private def post_params params.require(:post).permit(:title, :summary, :highlight, :image) end end
補足情報(FW/ツールのバージョンなど)
https://qiita.com/Kairi_Yasunnde/items/935dcdb8ec88b9ed9d91
この記事を参考にさせていただきました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。