我正在创建一个表单,用于向建议框添加建议。但是,虽然我能够成功提交表单并且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
但是,在显示提示已成功提交的Flash消息时,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>,
#]
有关为什么建议信息为零的任何想法,即使它似乎正在保存?
非常感谢您的帮助!
可能需要回答此问题的文件的摘录。我只包括了我认为的相关部分
SuggestionBox模型:
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
SuggestionBox控制器
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
答案 0 :(得分:1)
为什么要使用嵌套表单,因为你只创建了建议?
= form_for @suggestion do |f|
其他所有内容都应该保留不变,它应该有效。
顺便说一句,由于您不使用嵌套属性,因此accepts_nested_attributes_for :suggestions
模型中既不需要attr_accessible :suggestions_attributes
也不需要SuggestionBox
。