2013-03-22 3 views
0

컨트롤러와 뷰가 각각 두 가지 인 모델이 있습니다 (ProfileComment).하나의보기에서 두 모델의 데이터 표시

내 응용 프로그램의 전체보기 (전체 웹 페이지)는 Profileshow.html.erb입니다. 이 페이지에서 사용자는 belongs_to a Profile 인 설명을 작성할 수 있어야합니다.

표준 /comments/new 페이지로 이동하지 않고 어떻게이 작업을 수행 할 수 있습니까?

편집 :

def create @profile = profile.find(params[:profile_id]) @comment = @profile.comments.create(params[:comment]) redirect_to profile_path(@profile) 

CommentController

<%= simple_form_for([@profile, @profile.comment.build], html: {class: "form-inline"}) do |f| %> 
    <%= f.error_notification %> 

    <%= f.input :description, label: false, placeholder: 'Create an comment', input_html: { class: "span4" } %> 
    <%= f.submit 'Submit', class: 'btn btn-small'%> 

<% end %> 

을 그리고이 오류 받고 있어요 : 레일 가이드를 따른 후, 나는 구현

undefined method `comment' for #<Profile: 

고정을 : 빌드 문에서 필요한 의견은 당신이해야 할 모든 프로필 # 쇼에 코멘트 양식 코드를 추가입니다

@profile.comments.build 
+1

다음을보십시오 : http://guides.rubyonrails.org/getting_started.html#generating-a-controller – siekfried

+0

위의 오류를 참조하십시오 – mnort9

+0

양식 선언에's'을 (를) 잊어 버렸습니다 :'<% = simple_form_for ([@ profile, @ profile.comments.build] ' – siekfried

답변

1

복수가 될 수 있습니다. 그런 다음 profile_controller의 show 액션에 그런 짓을 :

def show 
@comment = Comment.new 
end 

그리고 코멘트 컨트롤러 추가에

:

def create 
@comment = Comment.create(params[:comment]) 
end 
+0

당신이 말하고자했던 첫 번째 방법은 show 액션 대신에 새로운 액션에 있다고 생각합니다. – GKnight

0
양식을 저장하고 AJAX 호출과 Knockout 같은 가능성이 무언가를 사용하여 페이지를 업데이트하는 것이 좋습니다

. 그래서 profiles/show.html.erb에서 코멘트를 게시하기 위해 규칙적인 (별도의) 양식을 만드십시오. jQuery 또는 이와 비슷한 것을 AJAX를 통해/comments에 게시하여 주석 컨트롤러에서 작성 작업을 수행합니다. 컨트롤러가 저장된 주석 또는 {: fieldname => 'too long'}과 같은 오류 메시지의 해시 중 하나 인 JSON 응답을 반환하도록합니다.

클라이언트에서 json 응답을 구문 분석하고 저장된 주석을 표시하거나 저장할 수없는 이유를 설명하는 오류 메시지를 표시하십시오. 이 모든 것을 일반 jQuery에서 할 수 있지만 Knockout과 같은 것을 추가하면 조금 더 간단해질 것입니다.

관련 문제