Reputation:
I am trying to get my submit button for to be on a different page but I get this error. I have the following 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 %>
This is in my posts_controller.rb,
class PostsController < ApplicationController
def index
@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 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
@post = Post.find(params[:id])
end
def update
@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
I have even tried rendering partials thinking that maybe it could help, I have tried several things overall but can't seem to figure this out. I am sure its very simple and I am missing it but if you know how to fix it then post your answer and thank you in advance! I can also add any other code you may need to see that can help this process!
Upvotes: 0
Views: 523
Reputation: 1003
f
is only available in the form_for
block, which is being prematurely closed with the first <% end %>
.
<% form_for @post do |f| %>
</div>
<% end %> <!-- Closes form_for; remove this line -->
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Edit:
<button type="button" class="btn btn-primary"><%= f.submit %></button>
in show.html.erb is also outside of the context of the form block. Move this line to _form.html.erb, before the closing <% end %>
and your error should disappear.
This will solve the error, but your form will not be output unless you modify the opening tag as Eifion suggested.
Upvotes: 2
Reputation: 5563
I think you should be using
<%= form_for @post do |f| %>
with an equals sign instead of
<% form_for @post do |f| %>
on the first line of your partial.
Upvotes: 0