【问题标题】:Rails form with nested resource is generating a nil output具有嵌套资源的 Rails 表单正在生成 nil 输出
【发布时间】:2013-06-06 21:49:02
【问题描述】:

我正在创建一个用于向建议框添加建议的表单。但是,虽然我能够成功提交表单,并且 rails 返回一条提示已成功提交建议的 flash 消息,但建议消息的值为 nil。

我已经查看了有关具有嵌套资源的表单的其他各种帖子,但没有任何帮助帮助我解决问题。

这就是我正在做的:

1.转到 localhost:3000/suggestion_boxes/1/suggestions/new

2。显示 app/views/suggestions/new.html.haml 表单:

%header
  %h1.title
    = @suggestion_box.name + " Suggestion Box"
.main
  = form_for ([@suggestion_box, @suggestion_box.suggestions.build]) do |f|
    - if @suggestion.errors.any?
      #error_explanation
        %ul
          - @suggestion.errors.messages.values.each do |msg|
            = msg.to_sentence
    .field
      %br/
      = f.text_area :suggestion_message, type:"text", placeholder:"Drop a note in our suggestion box...", :rows => 6, :cols => 30
    .actions
      = f.submit "Continue"

3.我在 :suggestion_message 字段中输入文本,然后单击提交。然后显示app/views/suggestion_boxes/show.html.haml:

%header
  %h1.title
    = @suggestion_box.name + " Suggestion Box"
%p#notice= notice
%p
  = link_to "Post a new suggestion", new_suggestion_box_suggestion_path(@suggestion_box)
%table
  %tr
    %th Message
    - @suggestions.each do |suggestion|
      %tr
        %td
          = suggestion.suggestion_message

然而,在此处显示建议已成功提交的快速消息时,suggest_message 表为空白。

这是我在数据库中查询此建议框的建议时得到的结果:

Suggestion Load (1.1ms)  SELECT "suggestions".* FROM "suggestions" WHERE
"suggestions"."suggestion_box_id" = 1 => [#<Suggestion id: 1, suggestion_message: nil, created_at: "2013-06-04 15:49:58", updated_at: "2013-06-04 15:49:58", anonymous_suggestion: nil, member_id: nil, suggestion_box_id: 1>,

#]

关于为什么建议消息是 nil 的任何想法,即使它看起来正在被保存?

非常感谢您的帮助!

可能需要回答这个问题的文件片段。我只包括了我认为相关的部分

建议框模型:

class SuggestionBox < ActiveRecord::Base
  attr_accessible :name , :suggestions_attributes
  belongs_to :organization
  has_many :suggestions, :dependent => :destroy 
  accepts_nested_attributes_for :suggestions
end

建议模型:

class Suggestion < ActiveRecord::Base
  attr_accessible :suggestion_message, :anonymous_suggestion
  belongs_to :suggestion_box
end

routes.rb

SuggestionBoxApp::Application.routes.draw do
  resources :invites, :organizations, :users, :sessions, :password_resets

  resources :suggestion_boxes do
    resources :suggestions
  end
end

建议控制器

class SuggestionsController < ApplicationController

  def new
    @suggestion_box = SuggestionBox.find(params[:suggestion_box_id])
    @suggestion = @suggestion_box.suggestions.build

    flash[:error] = "Sorry, no suggestion box found with the id #{:id}." and return unless @suggestion_box

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @suggestion }
    end
  end

  def create
    @suggestion_box = SuggestionBox.find(params[:suggestion_box_id])
    @suggestion = @suggestion_box.suggestions.new

    respond_to do |format|
      if @suggestion.save
        format.html { redirect_to suggestion_box_path(@suggestion_box), notice: 'Suggestion was successfully submitted.' }
        format.json { render json: @suggestion, status: :created }
      else
        format.html { render action: "new" }
        format.json { render json: @suggestion.errors, status: :unprocessable_entity }
      end
    end
  end
end

建议框控制器

  class SuggestionBoxesController < ApplicationController
    def show
      @suggestion_box = SuggestionBox.find(params[:id])
      @suggestions = @suggestion_box.suggestions.all

      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @suggestions }
      end
    end
  end

【问题讨论】:

    标签: ruby-on-rails ruby nested-resources


    【解决方案1】:

    既然只创建建议,为什么要使用嵌套表单?

    = form_for @suggestion do |f|
    

    其他一切都应该保持不变并且应该可以工作。

    顺便说一句,由于您不使用嵌套属性,因此您的SuggestionBox 模型中既不需要accepts_nested_attributes_for :suggestions,也不需要attr_accessible :suggestions_attributes。

    【讨论】:

    • 感谢您指出我不需要 Accept_nested_attributes。关于。使用嵌套形式,在我看来,将建议嵌套在建议框中是有意义的,这样可以轻松生成语义上有意义的 URL,并且还可以避免在添加新选择时必须选择建议框.但是,如果这些都不是很好的理由,或者嵌套资源通常不好,我将探索在没有嵌套资源的情况下解决这个问题。
    猜你喜欢
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多