我正在创建一个用于向建议框添加建议的表单。但是,虽然我能够成功提交表单并且 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
然而,在这里,虽然会显示一条提示已成功提交的消息,但Suggestion_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>,
#]
关于为什么建议消息为零的任何想法,即使它似乎正在被保存?
非常感谢您的帮助!
可能需要回答这个问题的文件片段。我只包括了我认为相关的部分
建议箱模型:
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
路线.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