前提・実現したいこと
ProgateのRubyonRailsのチュートリアル でTwitterのようなアプリを作成しました。そのアプリに画像を投稿できる機能をつけたいのですがつけ方がわからなくて困っています。
発生している問題・エラーメッセージ
https://nyoken.com/rails-carrierwave
上記のサイトを参考に進めているのですが、”⑤アップローダーを実装したいviewと、画像を表示したいviewを編集する”の箇所でエラーが発生してしまって止まっています。
Showing /home/vagrant/watashi_app/app/views/posts/new.html.erb where line #17 raised:
undefined local variable or method `f' for #<#Class:0x007fa214cbe5b8:0x007fa2152cddc8>
<input type="submit" value="投稿"> <%= f.label "画像をアップロード" %><br><%= f.file_field :image %> </div> </div>
該当のソースコード
#posts_controller.rb def new @post = Post.new end def create @post = Post.new( post_params ) if @post.save flash[:notice]="投稿を作成しました" redirect_to("/posts/index") else render("posts/new") end end private def post_params params.require(:post).permit(:content, :image) end
#new.html.erb <div class="main posts-new"> <div class="container"> <h1 class="form-heading">投稿する</h1> <%= form_tag("/posts/create")do %> <div class="form"> <div class="form-body"> <% @post.errors.full_messages.each do |message| %> <div class="form-error"> <%= message %> </div> <% end %> <textarea name="content" ><%= @post.content %></textarea> <input type="submit" value="投稿"> <%= f.label "画像をアップロード" %><br><%= f.file_field :image %> </div> </div> <% end%> </div> </div>
試したこと
new.html.erbのf.labelあたりの定義の仕方が間違っているとは思うのですがどのように変更すればいいかがわからない状態です。手詰まり状態で困っています。どなたか回答いただけると幸いです…
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。