rails中的嵌套资源不能保存

时间:2014-08-21 16:45:21

标签: ruby-on-rails nested-resources

我使用以下方法在Rails中设置了一些嵌套资源:

  resources :notes do
    resources :comments
  end

但即使/ notes / 1 / comments / new会将我带到一个新的评论页面,它也不会将评论的注释关联起来。我在评论中包含了一个note_id字段,但这不会填充。有什么提示吗?

评论控制员:

class CommentsController < ApplicationController
  before_action :set_comment, only: [:show, :edit, :update, :destroy]

  # GET /comments
  # GET /comments.json
  def index
    @comments = Comment.all
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
  end

  # GET /comments/new
  def new
    @comment = Comment.new
  end

  # GET /comments/1/edit
  def edit
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /comments/1
  # PATCH/PUT /comments/1.json
  def update
    respond_to do |format|
      if @comment.update(comment_params)
        format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
        format.json { render :show, status: :ok, location: @comment }
      else
        format.html { render :edit }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment.destroy
    respond_to do |format|
      format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_comment
      @comment = Comment.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def comment_params
      params.require(:comment).permit(:note_id, :comment)
    end
end

表格代码:

<%= form_for(@comment) do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
      <% @comment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :note_id %><br>
    <%= f.text_field :note_id %>
  </div>
  <div class="field">
    <%= f.label :comment %><br>
    <%= f.text_area :comment %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

您应该在控制器中创建这样的Comment

class CommentsController < ApplicationController
    before_action :set_comment, :set_note, only: [:show, :edit, :update, :destroy]

    # Build your comment from your @note
    # this sets all the association values correct (like ids)
    def create
        @comment = @note.comments.build(comment_params)
        # etc...
    end

    def set_note
        @note = Note.find(params[:note_id])
    end

    #...
end

因此,使用@note.commments.build可以帮助您解决问题。但是,当然,您需要使用@note哈希中的note_id来查询params