현재 Rails 4를 사용하여 웹 사이트를 만들고 있는데 다른 모델 "응답"을 사용하여 모델 "게시물"안에 모델 "후보"에 대한 양식을 만들어야합니다. .다른 컨트롤러의 쇼 내에서 컨트롤러 용 폼 만들기
이 포스트 모델 :
class Post < ActiveRecord::Base
validates :title, presence: true,
length: { minimum: 5 }
belongs_to :entrepreneur
belongs_to :categorie
has_many :candidatures
accepts_nested_attributes_for :candidatures
has_many :questions
accepts_nested_attributes_for :questions,
:reject_if => lambda { |a| a[:enonce].blank? },
:allow_destroy => true
end
후보 도시 모델 :
class Candidature < ActiveRecord::Base
has_many :reponses
accepts_nested_attributes_for :reponses, :allow_destroy => true
end
그리고 리스폰스 모델 :
class Reponse < ActiveRecord::Base
belongs_to :candidature
end
내가 입후보 및 reponse에 대한 컨트롤러가없는, 왜냐하면 나는 내가 필요하다고 생각하지 않기 때문이다. 프로젝트 인 게시물을 만들었고 게시물의보기보기에서 게스트가 답변을 통해 몇 가지 질문에 답변 할 수있는 양식을 만들어야하며 모든 답변을 하나의 후보로 저장해야합니다.
schema.rb은 다음과 같습니다 게시물에 대한
ActiveRecord::Schema.define(version: 20130718083016) do
create_table "candidatures", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "post_id"
end
create_table "questions", force: true do |t|
t.datetime "created_at"
t.datetime "updated_at"
t.integer "post_id"
t.text "enonce"
end
create_table "reponses", force: true do |t|
t.integer "candidature_id"
t.datetime "created_at"
t.datetime "updated_at"
t.text "enonce"
end
create_table "posts", force: true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
t.text "defi"
t.text "mission"
t.text "competences"
t.text "gain"
t.text "lieny"
t.text "liendb"
t.string "link"
end
컨트롤러를 :
class PostsController < ApplicationController
layout :resolve_layout
include Candidaturetopost
def new
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
flash[:notice] = "Successfully created project."
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
@post.candidatures.build
end
def index
@posts = Post.all
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :image, :defi, :mission, :competences, :gain, :lieny, :liendb, :link, questions_attributes: [:enonce, :post_id, :id, :_destroy],
candidatures_attributes: [:post_id, :id, reponses_attributes: [:candidature_id, :id, :enonce]])
end
내가 작업 얻기 위해 노력했다 쇼보기 다음으로
<%= form_for @post do |f| %>
<%= f.fields_for :candidatures do |cform| %>
<ol>
<% for question in @post.questions %>
<li><%= question.enonce %></li>
<%= cform.fields_for :reponses do |rform| %>
<%= rform.text_area :enonce %>
<% end %>
<% end %>
</ol>
<%= cform.submit %>
<% end %>
<% end %>
을 여기에 표시된 코드는 enonce에 대한 text_area도 표시되지 않습니다.
내가 할 수있는 일이 가능한 것일까 요? 비슷한 경우에는 어떻게 할 수 있습니까?
모델 구조가 복잡하기 때문에 다시 생각해 봐야합니다. –