I am trying to allow a user to click edit on their post and it pop up with the bootstrap option. It won't render the form and it gives me this error "undefined local variable or method `f' ".
This is the code I have in my show.html.erb
<div class="post-page">
<div class="panel panel-default">
<div class="panel-heading-gray">
<%= @user.name %> | <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">Edit<%= link_to edit_post_path(@post) %></button> | <%= link_to 'Delete', post_path, method: :delete, data: { Confirm: "Are you sure?" } %><i class="fa fa-times"></i>
</div>
<div class="panel-body"><h3><%= @post.body %></h3>
</div>
<div class="panel-footer">
Posted <%= time_ago_in_words(@post.created_at) %> Ago
</div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">Edit Post</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="message-text" class="control-label">Message:</label>
<%= render 'form' %>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit %>
</div>
</div>
</div>
</div>
</div>
This is in my _form.html.erb page,
<% form_for @post do |f| %>
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "Error") %> Prevent this post from posting</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I have the following in my post_controller.rb,
class PostsController < ApplicationController
def index
@user = User.find(session[:user_id])
@posts = Post.all.order("created_at DESC")
end
def welcome
@user = User.find(session[:user_id])
@post = Post.find(params[:id])
@posts = Post.order("created_at desc").limit(4).offset(1)
@signed_in_user = session[:user_id]
end
def posts
@user = User.find(session[:user_id]) unless session[:user_id] == nil
redirect_to login_path, notice: "You're not logged in" unless @user
end
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
@post.user_id = @signed_in_user
if @post.save
redirect_to dashboard_path
else
render 'new'
end
end
def show
@user = User.find(session[:user_id])
@post = Post.find(params[:id])
@posts = Post.order("created_at desc").limit(4).offset(1)
@signed_in_user = session[:user_id]
end
def edit
@user = User.find(session[:user_id])
@post = Post.find(params[:id])
end
def update
@user = User.find(session[:user_id])
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:body))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.user_id = @signed_in_user
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:body)
end
end
Also, This is on my edit.html.erb page,
<div id="post-page">
<h1>Edit Post</h1>
<%= render 'form' %>
</div>
form_for
requires that you use the <%=
syntax in your ERB. With the syntax you have (<%
), ERB will evaluate the statement, but will not print out the result (e.g., the rendered HTML).
Your tag should look like:
<%= form_for @post do |f| %>
As for the undefined local variable of method 'f'
, you have a stray <%= f.submit %>
in show.html.erb
, which is outside of the scope of the form. Because it is outside of the scope, the variable no longer exists and the error shows up.