我有两种形式(客户和帖子)。
以下是我的代码的一部分:
我的client.rb
class Client < ActiveRecord::Base
has_many:posts, dependent: :destroy
end
我的帖子.rb
class Post < ActiveRecord::Base
belongs_to :client
end
我的客户端控制器
class ClientsController < ApplicationController
before_action :authenticate_user!
before_action :find_client, only:[:show, :edit, :update]
... is working fine
我的帖子控制器
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
我的帖子的'index.html.erb'
<h2>Posts of <%=@client.name%></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>
我的部分'_editform.html.erb'
<h2>Edit Post of <%=@client.name%></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 %>
我的'edit.html.erb'
<%= render 'editform'%>
答案 0 :(得分:0)
您的帖子控制器中的更新操作没有通过新提交的参数进行保存。你需要传递新的参数,如
def update
@post = @client.posts.find(params[:id])
if @post.update(post_params)
flash[:notice]="Post Updated Sucessfully"
redirect_to client_post_path(@client, @post)
else
render 'edit'
end
end
private
def post_params
params.require(:post).permit(:post_name, :title, :body)
end
这样的事情,所以你可以更新你的帖子。