| 12 сентября 2010, 16:08 | Несколько вопросов |
|---|---|
artyomst Живет: не указан Сообщений: 22 Рейтинг: 0.0 Рег: 12 февр. 2010 |
|
class Question < ActiveRecord::Base has_many :answers end class Answer < ActiveRecord::Base belongs_to :question end |
|
| модели связи формы |
| 12 сентября 2010, 17:50 | RE: Несколько вопросов |
|---|---|
artyomst Живет: не указан Сообщений: 22 Рейтинг: 0.0 Рег: 12 февр. 2010 |
|
| 12 сентября 2010, 18:01 | RE: RE: Несколько вопросов |
|---|---|
Ruslan Voloshin Живет: Odessa, UKR Сообщений: 2916 Рейтинг: 762.0 Рег: 13 марта 2007 Его блог |
|
| Ищу rails разработчиков. | |
| 12 сентября 2010, 18:16 | RE: Несколько вопросов |
|---|---|
artyomst Живет: не указан Сообщений: 22 Рейтинг: 0.0 Рег: 12 февр. 2010 |
|
class SurveysController < ApplicationController def index @surveys = Survey.all end def show @survey = Survey.find(params[:id]) end def new @survey = Survey.new 3.times do question = @survey.questions.build 4.times { question.answers.build } end end def create @survey = Survey.new(params[:survey]) if @survey.save flash[:notice] = "Successfully created survey." redirect_to @survey else render :action => 'new' end end def edit @survey = Survey.find(params[:id]) end def update @survey = Survey.find(params[:id]) if @survey.update_attributes(params[:survey]) flash[:notice] = "Successfully updated survey." redirect_to @survey else render :action => 'edit' end end def destroy @survey = Survey.find(params[:id]) @survey.destroy flash[:notice] = "Successfully destroyed survey." redirect_to surveys_url end end class Answer < ActiveRecord::Base belongs_to :question end class Question < ActiveRecord::Base belongs_to :survey has_many :answers, :dependent => :destroy accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true end class Survey < ActiveRecord::Base has_many :questions, :dependent => :destroy accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true end <p class="fields"> <%= f.label :content, "Answer" %> <%= f.text_field :content %> <%= link_to_remove_fields "remove", f %> </p> <% form_for @survey do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <% f.fields_for :questions do |builder| %> <%= render "question_fields", :f => builder %> <% end %> <p><%= link_to_add_fields "Add Question", f, :questions %></p> <p><%= f.submit "Submit" %></p> <% end %> <div class="fields"> <p> <%= f.label :content, "Question" %> <%= link_to_remove_fields "remove", f %><br /> <%= f.text_area :content, :rows => 3 %> </p> <% f.fields_for :answers do |builder| %> <%= render 'answer_fields', :f => builder %> <% end %> <p><%= link_to_add_fields "Add Answer", f, :answers %></p> </div> <% title "Edit Survey" %> <%= render :partial => 'form' %> <p> <%= link_to "Show", @survey %> | <%= link_to "View All", surveys_path %> </p> <% title "Surveys" %> <table> <tr> <th>Name</th> </tr> <% for survey in @surveys %> <tr> <td><%=h survey.name %></td> <td><%= link_to "Show", survey %></td> <td><%= link_to "Edit", edit_survey_path(survey) %></td> <td><%= link_to "Destroy", survey, :confirm => 'Are you sure?', :method => :delete %></td> </tr> <% end %> </table> <p><%= link_to "New Survey", new_survey_path %></p> <% title "New Survey" %> <%= render :partial => 'form' %> <p><%= link_to "Back to List", surveys_path %></p> <% title "Survey" %> <p> <strong>Name:</strong> <%=h @survey.name %> </p> <ol> <% for question in @survey.questions %> <li> <%=h question.content %> <ul> <% for answer in question.answers %> <li><%=h answer.content %></li> <% end %> </ul> </li> <% end %> </ol> <p> <%= link_to "Edit", edit_survey_path(@survey) %> | <%= link_to "Destroy", @survey, :confirm => 'Are you sure?', :method => :delete %> | <%= link_to "View All", surveys_path %> </p> function remove_fields(link) { $(link).previous("input[type=hidden]").value = "1"; $(link).up(".fields").hide(); } function add_fields(link, association, content) { var new_id = new Date().getTime(); var regexp = new RegExp("new_" + association, "g") $(link).up().insert({ before: content.replace(regexp, new_id) }); } function remove_fields(link) { $(link).prev("input[type=hidden]").val("1"); $(link).closest(".fields").hide(); } function add_fields(link, association, content) { var new_id = new Date().getTime(); var regexp = new RegExp("new_" + association, "g") $(link).before(content.replace(regexp, new_id)) } |
|
| 12 сентября 2010, 21:09 | RE: Несколько вопросов |
|---|---|
Ruslan Voloshin Живет: Odessa, UKR Сообщений: 2916 Рейтинг: 762.0 Рег: 13 марта 2007 Его блог |
|
| Ищу rails разработчиков. | |
| 12 сентября 2010, 21:49 | RE: RE: Несколько вопросов |
|---|---|
Ігор Касянчук Живет: Ivano-Frankivsk,UKR Сообщений: 415 Рейтинг: 184.0 Рег: 05 сент. 2007 |
|
| kartaonline.com | |
| 13 сентября 2010, 11:17 | RE: RE: Несколько вопросов |
|---|---|
artyomst Живет: не указан Сообщений: 22 Рейтинг: 0.0 Рег: 12 февр. 2010 |
|
<% form_for @survey do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <% f.fields_for :questions do |builder| %> <%= render "question_fields", :f => builder %> <% end %> <p><%= link_to_add_fields "Add Question", f, :questions %></p> <p><%= f.submit "Submit" %></p> <% end %> <div class="fields"> <p> <%= f.label :content, "Question" %> <%= link_to_remove_fields "remove", f %><br /> <%= f.text_area :content, :rows => 3 %> </p> <% f.fields_for :answers do |builder| %> <%= render 'answer_fields', :f => builder %> <% end %> <p><%= link_to_add_fields "Add Answer", f, :answers %></p> </div> |
|
| 13 сентября 2010, 12:03 | RE: RE: RE: Несколько вопросов |
|---|---|
Ruslan Voloshin Живет: Odessa, UKR Сообщений: 2916 Рейтинг: 762.0 Рег: 13 марта 2007 Его блог |
|
| Ищу rails разработчиков. | |
| 13 сентября 2010, 12:11 | RE: RE: RE: RE: Несколько вопросов |
|---|---|
artyomst Живет: не указан Сообщений: 22 Рейтинг: 0.0 Рег: 12 февр. 2010 |
|
# Methods added to this helper will be available to all templates in the application. module ApplicationHelper def link_to_remove_fields(name, f) f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") end def link_to_add_fields(name, f, association) new_object = f.object.class.reflect_on_association(association).klass.new fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| render(association.to_s.singularize + "_fields", :f => builder) end link_to_function(name, h("add_fields(this, '#{association}', '#{escape_javascript(fields)}')")) end end |
|
| 13 сентября 2010, 13:12 | RE: RE: RE: RE: RE: Несколько вопросов |
|---|---|
Ruslan Voloshin Живет: Odessa, UKR Сообщений: 2916 Рейтинг: 762.0 Рег: 13 марта 2007 Его блог |
|
link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}'"))link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}'".html_safe)) |
|
| Ищу rails разработчиков. | |
| 13 сентября 2010, 13:15 | RE: RE: RE: RE: RE: RE: Несколько вопросов |
|---|---|
artyomst Живет: не указан Сообщений: 22 Рейтинг: 0.0 Рег: 12 февр. 2010 |
|