Ruby on rails 4.1 Rails嵌套资源未更新
我有两个表格(客户和帖子)Ruby on rails 4.1 Rails嵌套资源未更新,ruby-on-rails-4.1,Ruby On Rails 4.1,我有两个表格(客户和帖子) 对于我的客户机,我可以毫无问题地创建/显示/更新/删除我的客户机 对于我的帖子,我可以创建/显示和删除。但由于某些原因,它没有更新。 在更新表单中,在我进行更改并单击“更新帖子”按钮后,它显示为好像它已更新,但当我检查帖子时,它仍保留旧数据。我不知道会出什么问题 以下是我的部分代码: 我的客户.rb class Client < ActiveRecord::Base has_many:posts, dependent: :destroy end cla
- 对于我的客户机,我可以毫无问题地创建/显示/更新/删除我的客户机
- 对于我的帖子,我可以创建/显示和删除。但由于某些原因,它没有更新。 在更新表单中,在我进行更改并单击“更新帖子”按钮后,它显示为好像它已更新,但当我检查帖子时,它仍保留旧数据。我不知道会出什么问题
class Client < ActiveRecord::Base
has_many:posts, dependent: :destroy
end
class客户端
我的post.rb
class Post < ActiveRecord::Base
belongs_to :client
end
class Post
我的客户控制器
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
class客户端控制器
我的邮政管理员
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
class PostsController
我的文章的'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>
职位名称职位名称
|
我的部分“\u 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 %>
<%= render 'editform'%>
编辑文章
我的“edit.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 %>
<%= render 'editform'%>
您在post控制器中的更新操作没有传入要保存的新提交参数。你需要像这样传递新的参数
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
类似这样的东西,所以你可以更新你的帖子