Not sure why the VIEW does't work: Rails 4 nested attributes and has_many :through associaton in a form -
i followed page build app:
rails 4 nested attributes , has_many :through associaton in form
but shows nothing in view:
(the weird thing when typed "f.fields_for :questionnaire_surverys |ff|" instead of right one, showed me ocrrect page.
any suggestions appreciated.
here models:
questionnaire.rb
class questionnaire < activerecord::base has_many :questionnaire_surveys has_many :surveys, through: :questionnaire_surveys accepts_nested_attributes_for :questionnaire_surveys end
questionnaire_survey.rb
class questionnairesurvey < activerecord::base belongs_to :questionnaire belongs_to :survey accepts_nested_attributes_for :survey end
survey.rb
class survey < activerecord::base has_many :questions, :dependent => :destroy accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true has_many :questionnaire_surveys has_many :questionnaires, through: :questionnaire_surveys end
and questionnaire_controller.rb
def new @questionnaire = questionnaire.new @surveys = survey.all end def questionnaire_params params.require(:questionnaire).permit(:name, questionnaire_surveys_attributes: [:id, survey_attributes:[:id]]) end
this _form.html.erb
<%= form_for(@questionnaire) |f| %> <p> <%= f.label :name %><br/> <%= f.text_field :name %> <div class="field"> <%= f.fields_for :questionnaire_surveys |ff| %> <%= ff.fields_for :survey |builder| %> <% @surveys.each |survey| %> <%= builder.check_box :id, {}, survey.id %> <%= builder.label survey.name %> <% end %> <% end %> <% end %> </div> </p> <div class="actions"> <%= f.submit %> </div>
updated:
started post "/questionnaires" ::1 @ 2015-07-29 22:45:16 +0800 processing questionnairescontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"k4skrc08pwahao1iermqckssdqzygf+uhwofpdelbxo0o4/psy3y7i/krqa01omtoq4vllt/yqdnkcbplgp86w==", "questionnaire"=>{"name"=>"what happened", "questionnaire_surveys_attributes"=>{"0"=>{"survey_attributes"=>{"name"=>""}}}}, "commit"=>"create questionnaire"} unpermitted parameter: name (0.1ms) begin transaction sql (0.7ms) insert "questionnaires" ("name", "created_at", "updated_at") values (?, ?, ?) [["name", "what happened"], ["created_at", "2015-07-29 14:45:16.374246"], ["updated_at", "2015-07-29 14:45:16.374246"]] sql (0.2ms) insert "surveys" ("created_at", "updated_at") values (?, ?) [["created_at", "2015-07-29 14:45:16.377439"], ["updated_at", "2015-07-29 14:45:16.377439"]] sql (0.1ms) insert "questionnaire_surveys" ("questionnaire_id", "survey_id", "created_at", "updated_at") values (?, ?, ?, ?) [["questionnaire_id", "52"], ["survey_id", "38"], ["created_at", "2015-07-29 14:45:16.378845"], ["updated_at", "2015-07-29 14:45:16.378845"]] (0.9ms) commit transaction redirected http://localhost:3000/questionnaires/52 completed 302 found in 12ms (activerecord: 2.0ms)
update - 2015/7/31
started post "/questionnaires" ::1 @ 2015-07-31 17:46:50 +0800 processing questionnairescontroller#create html parameters: {"utf8"=>"✓", "authenticity_token"=>"t/00priclauvdqpfxonktaxrphtdy082pavhb/vqso4qqh8llrnz6z2qg6fhjv3urnnepn6d0zjukb67drfzfw==", "questionnaire"=>{"name"=>"omg", "questionnaire_surveys_attributes"=>{"0"=>{"survey_attributes"=>{"name"=>""}}}}, "commit"=>"create questionnaire"} (0.2ms) begin transaction sql (0.7ms) insert "questionnaires" ("name", "created_at", "updated_at") values (?, ?, ?) [["name", "omg"], ["created_at", "2015-07-31 09:46:50.440466"], ["updated_at", "2015-07-31 09:46:50.440466"]] sql (0.4ms) insert "surveys" ("name", "created_at", "updated_at") values (?, ?, ?) [["name", ""], ["created_at", "2015-07-31 09:46:50.446176"], ["updated_at", "2015-07-31 09:46:50.446176"]] sql (0.2ms) insert "questionnaire_surveys" ("questionnaire_id", "survey_id", "created_at", "updated_at") values (?, ?, ?, ?) [["questionnaire_id", "53"], ["survey_id", "39"], ["created_at", "2015-07-31 09:46:50.450001"], ["updated_at", "2015-07-31 09:46:50.450001"]] (0.9ms) commit transaction redirected http://localhost:3000/questionnaires/53 completed 302 found in 22ms (activerecord: 2.4ms)
update - 2015/8/05
i can't upload pics here, hope need:
<input placeholder="vision" type="text" name="questionnaire[questionnaire_surveys_attributes][0][survey_attributes][name]" id="questionnaire_questionnaire_surveys_attributes_0_survey_attributes_name">
update - 2015/8/11
_form.erb.html
<div class="field"> <% @surveys.each |survey| %> <%= check_box_tag "questionnaire[questionnaire_surveys_attributes][][survey_id]", survey.id %> <%= label_tag survey.name %> <% end %> </div>
questionnaires_controller.rb
params.require(:questionnaire).permit(:name, questionnaire_surveys_attributes: [:survey_id]) def new @questionnaire = questionnaire.new @surveys = survey.all end
update - 2015/8/17
i misused has_many :through
, accepts_nested_attributes_for
. in has_many:xxx :through
case, there xxx_ids
. in accepts_nested_attributes_for xxx
case, there xxx_attributes
.
i used accepts_nested_attributes_for
in both questionnaire.rb
and questionnaire_survey.rb
, mistake.
the correct way want use has_many :through
only. questionnaire_controller.rb
have
def questionnaire_params params.require(:questionnaire).permit(:name, :survey_id=>[]) end
in _form view, should
<%= check_box_tag "questionnaire[survey_id][]", survey.id %>
it's easier now.
@rich peck help.
first things first - if you're not seeing form elements appear, it's because you've not got set correctly in backend.
for longest time, tried set , getting frustrated embedded form not appear. wasn't until sorted out worked. it's called graceful degradation (i think) - whereby no error appear, yet functionality impaired.
firstly, think haven't built associated objects in controller:
#app/controllers/questionnaire_controller.rb def new @questionnaire = questionnaire.new # need build associated objects, this: @questionnaire.questionnaire_surveys.build.build_survey @surveys = survey.all end
--
secondly, there better way show checkboxes @surveys
object:
<%= ff.fields_for :survey |survey| %> <%= survey.collection_check_boxes :survey_ids, @surveys, :id, :name %> <% end %>
you can read collection_check_boxes
here
--
thirdly, should learn haml. write entire form this:
= form_for @questionnaire |f| .name = f.label :name = f.text_field :name .field = f.fields_for :questionnaire_surveys |ff| %> = ff.fields_for :survey |survey| %> = survey.collection_check_boxes :survey_ids, @surveys, :id, :name .actions = f.submit
--
finally, don't use html elements styling.
<p>
& <br>
should used markup. if you're using them styling effect, you'll end causing problems browser compatibility etc.
you need let css styling (colouring, size, position), , on-page elements used ways separate content of application.
update
okay, i've looked @ bitbucket:
- you need uncomment
@questionnaire.questionnaire_surveys.build.build_survey
inapp/controllers/questionnaires_controller.rb#20
if that, should work.
i cannot see problems construct of models , controllers. sure you've refreshed etc?
i see you're calling <%= render "form" %>
- try putting form directly in new
view test if work.
also, have tried using simple way add fields, this:
<%= f.fields_for :questionnaire_surveys |ff| %> <%= ff.fields_for :survey |builder| %> <% @surveys.each |survey| %> <%= builder.text_field :name, placeholder: survey.name %> <% end %> <% end %>
finally, if post posted parameters after form submit, i'll in stronger position see of errors/problems may have.
--
you can change params following:
#app/controllers/questionnaires_controller.rb ... def questionnaire_params params.require(:questionnaire).permit(:name, questionnaire_surveys_attributes: [:id, survey_attributes:[:name]]) end
Comments
Post a Comment