2

I am currently using Rails 4 to create a website and I need to create a form for a model "candidature" inside a model "post" using another model "reponse".

This the post model :

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

The candidature model :

class Candidature < ActiveRecord::Base
has_many :reponses
accepts_nested_attributes_for :reponses, :allow_destroy => true
end

And the reponse model :

class Reponse < ActiveRecord::Base
belongs_to :candidature
end

I don't have controllers for candidature and reponse, because i don't think i need any (do correct me if I'm wrong). I have created posts that are projects and in the show view of posts, i need to create a form where a guest can answer some questions through answer and all those answers have to be saved in one candidature.

The schema.rb looks like that :

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

The controller for post :

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

The show view i have tried to get working :

<%= 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 %>

With the code shown here, the text_area for enonce isn't even displayed.

Is what I want to do even possible ? How can I have something similar if not ?

4

2 回答 2

0

你是对的,它太复杂了。我用来帮助我让它工作,我现在有了我想要的三个模型之间的链接。

于 2013-07-19T10:36:43.773 回答
0

尝试这个

@post.candidatures.build
@post.candidatures.each {|candidature| candidature.responses.build }

但是正如我在评论中所写的那样,您使模型结构过于复杂,我相信您应该重新考虑它。

于 2013-07-18T11:36:19.867 回答