【问题标题】:Rails nested resource not updatingRails嵌套资源不更新
【发布时间】:2016-05-29 10:57:45
【问题描述】:

我有两种形式(客户和帖子)。

  • 对于我的客户,我可以毫无问题地创建/显示/更新/删除我的客户。
  • 对于我的帖子,我可以创建/显示和删除。但由于某种原因,它没有更新。 在更新表单中,我进行更改并单击“更新帖子”按钮后,它显示好像它已更新,但当我检查帖子时,它仍然有旧数据。我不知道可能出了什么问题。

这是我的代码的一部分:

我的客户.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'%>

【问题讨论】:

    标签: ruby-on-rails-4.1


    【解决方案1】:

    您在 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
    

    类似这样,所以你可以更新你的帖子。

    【讨论】:

      最近更新 更多