2013-05-11 16 views
3

시나리오를 설정하고 세부 사항을 작성합니다. 나는 새로운 작성하는 작업과 접촉 컨트롤러를 가지고컨트롤러 동작에서 레일이 형성되고 다른 뷰에서 렌더링됩니까?

: 여기

는 시나리오입니다. 이것은 사용자가 작성하고 응답을 저장하기를 원하는 양식입니다.

연락 컨트롤러 :

class ContactController < ApplicationController 

    def new 
    @contact = Contact.new 
    end 

    def create 
    @contact = Contact.new(params[:contact]) 
    respond_to do |format| 
     if @contact.save 
     format.html { redirect_to social_path, notice: 'Message sent successfully!' } 
     else 
     format.html { render action: 'new' } 
     end 
    end 
    end 
end 

I가 당신이하는 것처럼, 부분적인 양식을 렌더링 메신저 해당 부분 _form.html.erb뿐만 아니라 new.html.erb보기. 이러한보기는 모두 연락처보기 폴더 내에 있으며 해당 경로가 있습니다.

_form.html.erb

<%= form_for @contact do |f| %> 
    <% if @contact.errors.any? %> 
    <h2> 
     <%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved: 
    </h2> 

    <ul> 
    <% @contact.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    <% end %> 
<div class="row collapse"> 
    <div class="large-2 columns"> 
    <%= f.label :name, :class => 'inline' %> 
    </div> 
    <div class="large-10 columns"> 
    <%= f.text_field :name, :placeholder => 'Kenny Powers' %> 
    </div> 
</div> 
<div class="row collapse"> 
    <div class="large-2 columns"> 
    <%= f.label :subject %> 
    </div> 
    <div class="large-10 columns"> 
    <%= f.text_field :subject %> 
    </div> 
</div> 
<div class="row collapse"> 
    <div class="large-2 columns"> 
    <%= f.label :email %> 
    </div> 
    <div class="large-10 columns"> 
    <%= f.text_field :email, :placeholder => '[email protected]' %> 
    </div> 
</div> 
<div class="row collapse"> 
    <div class="large-2 columns"> 
    <%= f.label :message %> 
    </div> 
    <div class="large-10 columns"> 
    <%= f.text_area :message %> 
    </div> 
</div> 
<div class="row collapse"> 
    <div class="large-2 columns end"> 
    <%= f.submit %> 
    </div> 
</div> 
<% end %> 

new.html.erb <퍼센트 = 렌더링 "접촉/양식 '%>

I는 I 같은 static_pages 제어기를 작성한 정적 콘텐츠 페이지를 동적 콘텐츠를로드하는 페이지와 별도로 유지하려고합니다.

static_pages 컨트롤러의 내부에는 비어있는 소셜 컨트롤러 액션이 있습니다.

match 'social' => 'static_pages#social' 

social.html.erb

<%= render :template => 'contact/new', :@contact => Contact.new %> 

static_pages_controller 다음과 같이 내가 static_pages에 위치한 social.html.erb,에서 정적 콘텐츠를 표시하고있어 일치하는 경로와 폴더를 볼

def social 

end 

멋진 페이지는 완벽하게 렌더링됩니다.

가 어떻게 사회적 페이지에서 문의 양식을 렌더링 할 : 부품 메신저 지금

는 혼동? 그것은 social.html.erb 파일에 앉아 나는/나에게

ActionView::MissingTemplate in Static_pages#social 

Showing app/views/contact/new.html.erb where line #1 raised: 

1: <%= render 'form' %> 

을 제공

<%= render template: 'contact/new' %>

그것은 접촉에 _form.html.erb에 대한 누락 된 템플릿 오류를 던지고있다 new.html.erb. 내가

<%= render 'contact/form' %> 

을 추가하여 양식 위치를 지정하려고하면 나는 undefined method model_name for NilClass:Class 오류가 발생합니다.

내가 완전히 잘못하고 있습니까? 제가 시도하고있는 것을하기위한 더 좋은 방법이 있습니까? 누군가 평신도 설명을 해줄 수 있습니까?

폼 부분을 다른 컨트롤러로 렌더링 할 때 비슷한 질문이 있지만 한 가지 해결책 일뿐입니다.나는 이것이 일어나고 있는지, 또는 일을하는 더 좋고/올바른 방법을지지하는 말을지지하는 말에 크게 감사 할 것입니다.

자세한 정보/특정 코드가 필요하면 알려주십시오.

+0

입니다. 마지막 오류 메시지에 도움이 될 줄 번호가 있습니까? – Baldrick

+0

이 질문을 참조하십시오 : http://stackoverflow.com/questions/9304368/rails-3-render-partial-from-another-controller-error-actionviewmissingtempla - 양식을 렌더링 할 때 사용되는 모든 객체를 전달해야 할 수도 있습니다. 그 부분. – mccannf

+0

컨트롤러 코드와 _form 코드를 모두 추가 할 수 있습니까? 우리가 코드 – fontno

답변

2

솔루션은 다음과 같습니다

당신의 "/contact/new.html.erb"

다음에 특정 렌더링 변경에 복사 및 Paster 당신의 "social.html.erb을"

<%= render 'form' %> # that's ok now, because you are in the right directory 
이제 routes.rb에서

는 :

resources :contacts  # it's important to add this for RESTful architecture 

match 'social' => 'contact#new' # and DELETE the other line with => 'contact#index' because it is no more necessary 

을였습니다.

UPDATE : 여기 어쩌면`_form.html.erb` 로컬 변수 또는 초기화되지 않은 인스턴스 변수를 사용 (응용 프로그램에서) 문제의 내 GitHub의-솔루션

https://github.com/rubybrah/solution 
+0

이것은 설명이 아니라 대답입니다. –

+0

나는 대답 할 수 없었고, 돕고 싶다. 내가 뭘 할 수 있을까? – rubybrah

+0

의견이나 아무 것도 없습니다. 답변은 답변으로되어 있습니다. 그것은 포럼이 아니라 Q & A입니다. –

관련 문제