2013-06-06 2 views
0

제안 상자에 제안 사항을 추가하기위한 양식을 작성 중입니다. 그러나 양식을 성공적으로 제출할 수 있고 레일스가 제안이 성공적으로 제출되었다는 플래시 메시지를 반환하는 동안 제안 메시지의 값은 nil입니다.중첩 된 리소스가있는 레일스 양식이 출력 없음을 생성 중입니다.

중첩 된 리소스가있는 양식에 대한 여러 가지 다른 게시물을 검토했지만 문제 해결에 도움이 된 것은 없습니다.

1.은 localhost로 : 새로운 3000/suggestion_boxes/1/제안/

2. 양식 응용 프로그램/의견/제안/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 필드를하고 제출을 클릭합니다. 그런 다음 응용 프로그램/뷰/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 

제안 모델 : 난 단지 내가

SuggestionBox 모델 관련 부분이 될 것이라고 생각 무엇을 포함 시켰습니다

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 

답변

1

중첩 된 양식을 사용하는 이유, 이후 당신은 만들 제안?

= form_for @suggestion do |f| 

다른 모든 것은 변경하지 않고 그대로 두어야하며 작동해야합니다.

현재 중첩 된 특성을 사용하지 않으므로 SuggestionBox 모델에는 accepts_nested_attributes_for :suggestions 또는 attr_accessible :suggestions_attributes이 필요하지 않습니다.

+0

accepts_nested_attributes가 필요 없음을 지적 해 주셔서 감사합니다. 레. 중첩 된 양식을 사용하면 의미 상자가 의미있는 URL을 쉽게 생성하고 새 선택을 추가 할 때 추천 상자를 선택하지 않아도되도록 제안 상자 내에서 제안을 중첩하는 것이 좋습니다. . 그러나 그러한 이유가 좋지 않거나 중첩 된 리소스가 일반적으로 나쁘다면 중첩 된 리소스가 없어도 접근 할 수 있습니다. – andersr