I have two forms (Clients and Posts).
- For my client, I can create/show/update/delete my client with no problem.
- For my post, I can create/show and delete. But for some reason it does not update. In the update form, after I make the changes and click on the "Update Post" button, it shows as if it was updated, but when I check the post, it still has the old data. I do not know what could be wrong.
Here is part of my code:
My client.rb
class Client < ActiveRecord::Base
has_many:posts, dependent: :destroy
end
My post.rb
class Post < ActiveRecord::Base
belongs_to :client
end
My client controller
class ClientsController < ApplicationController
before_action :authenticate_user!
before_action :find_client, only:[:show, :edit, :update]
... is working fine
My post controller
class PostsController < ApplicationController
before_action :authenticate_user!
before_action :find_client
before_action :find_post, only:[:show, :edit, :update, :destroy]
def index
@client = Client.find(params[:client_id])
@posts = @client.posts
end
def new
@post = @client.posts.new
end
def create
@post = @client.posts.create(post_param)
flash[:notice]="Post Created Sucessfully."
redirect_to client_post_path(@client, @post)
end
def update
# @post = @client.posts.find(params[:id])
if @post.save
flash[:notice]="Post Updated Sucessfully"
redirect_to client_post_path(@client, @post)
else
render 'edit'
end
end
def destroy
@post.destroy
flash[:notice]="Post Deleted Sucessfully"
redirect_to client_posts_path(@client)
end
private
def find_client
@client = Client.find(params[:client_id])
end
def find_post
@post = Post.find(params[:id])
end
def post_param
params.require(:post).permit(:post_name, :title, :body)
end
end
My 'index.html.erb' for my posts
<h2>Posts of <%[email protected]%></h2>
<div class="NewItem">
<p><%= link_to "NEW POST", new_client_post_path(@client)%></p>
</div>
<div class="ListPosts">
<table border="1">
<th><td><b>Post Name</b></td><td><b>Post Title</b></td></th>
<% @client.posts.each do |p|%>
<tr>
<td><%= link_to "Edit", edit_client_post_path(@client,p) %> | <%=link_to "Delete", [@client, p], method: :delete, data:{Confirm: "Confirm Data Exclusion?"}%></td>
<td><%=p.post_name%></td>
<td><%=p.title%></td>
</tr>
<% end %>
</table>
</div>
My partial '_editform.html.erb'
<h2>Edit Post of <%[email protected]%></h2>
<%= form_for [@client, @post] do |f|%>
<%=f.label:post_name %><br>
<%=f.text_field:post_name %><br>
<%=f.label:title %><br>
<%=f.text_field:title %><br>
<%=f.label:body %><br>
<%=f.text_area:body %><br>
<%=f.submit %><br>
<% end %>
My 'edit.html.erb'
<%= render 'editform'%>
Your update action in your post controller didn't pass in the new submitted params to save. You need to pass in the new params like
something like this, so you can update your post.