Rails: Passing nested attributes to vue.js - ruby-on-rails

I have a Survey model
class Survey < ApplicationRecord
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions, allow_destroy: true
belongs_to :user
end
And Quesiton model which is nested to "survey" as below;
class Question < ApplicationRecord
enum qtype: [:multiple_choice, :check_boxes, :short_answer]
belongs_to :survey
has_many :options
accepts_nested_attributes_for :options, allow_destroy: true
end
And option model connected to question as nested.
class Option < ApplicationRecord
belongs_to :question
end
Parameters in my surveys_controller:
def survey_params
params.require(:survey).permit(:name, questions_attributes:[:id, :title, :qtype, :_destroy, options_attributes:[:id, :otext, _destroy]])
end
It is working clear Without Vue.js implementing.
I want to use vue.js for making dynamic form.
What should i do passing do form all nested attributes Without using 'gon' gem
also which json file should i create with jbuilder?
Thanks

Related

How do I permit params for my active admin resource with a has_many_through association?

I'm having trouble setting my permitted params in active admin. The docs say "Any form field that sends multiple values (such as a HABTM association, or an array attribute) needs to pass an empty array to permit_params". Here is my code from admin/sample.rb:
ActiveAdmin.register Sample do
permit_params :title, :description, :file_type, :audio_data, :channels, :sample_rate, :file_size,
categories: []
end
When I try this all the attributes appear in my active admin table but there is nothing for categories. I'm not really sure where I'm going wrong. I've tried category_ids and sample_categories too but still it doesn't show in the table. I've also tried adding an attribute to the array, such as categories: [:name] but still nothing.
Everything behaves as it should in the app itself and I'm able to add a category when creating a sample, for example. Any suggestions about how to solve this? Here is some relevant code.
models/sample.rb
class Sample < ApplicationRecord
...
has_many :sample_categories
has_many :categories, through: :sample_categories
end
models/category.rb
class Category < ApplicationRecord
has_many :sample_categories
has_many :samples, through: :sample_categories
end
models/sample_category.rb
class SampleCategory < ApplicationRecord
belongs_to :sample
belongs_to :category
end
controllers/samples_controller.rb
...
def sample_params
params.require(:sample).permit(:title, :description, :audio, :file_type, :file_size, :sample_rate, :channels, :tag_list, category_ids: [])
end
...
Have you tried the accepts_nested_attributes_for function in your Sample model?
class Sample < ApplicationRecord
...
has_many :sample_categories
has_many :categories, through: :sample_categories
accepts_nested_attributes_for :categories
end
Try first using test or rails console if it works.
Also please update your admin/sample.rb to allow nested attributes from categories
ActiveAdmin.register Sample do
permit_params :title, :description, :file_type, :audio_data, :channels, :sample_rate, :file_size,
categories_attributes: [:id, :name, :etc]
end
Note:
if you are using active admin, the samples_controller.rb CRUD controller declaration is not required

Serialize nested attributes in Rails?

So I have models like this:
folder, up_file has 1 chat_room
chat_room belongs to crmable with polymorphic: true (so 1 chat_room may belongs to folder or 1 up_file)
chat_room has many comments
comment belongs to chat_room and user (who posted the comment)
I've defined all relationship like this:
class Folder < ApplicationRecord
has_one :chat_room, as: :crmable, dependent: :destroy
end
class UpFile < ApplicationRecord
has_one :chat_room, as: :crmable, dependent: :destroy
end
class ChatRoom < ApplicationRecord
belongs_to :crmable, polymorphic: true
has_many :comments, dependent: :destroy
has_many :users, through: :comments
end
class Comment < ApplicationRecord
belongs_to :chat_room
belongs_to :user
end
This setup worked fine with my web app. However, I'm writing API for this app, and I can't get that nested associations serialized with this gem
https://github.com/rails-api/active_model_serializers
I've follow their tutorial here but my associations not rendered.
class FolderSerializer < ActiveModel::Serializer
attributes :id, :name, :ancestry, :creator_name #... my other attributes
has_one :chat_room, include: [ :id, comments: :user ]
# ^ I tried to serialize folder's chat_room with all it comments, along with these comments' users.
end
All I got is this - chat_room association not serialized?

Mass edit (create/destroy) many2many relation on Rails

I'm stucked in the way to do this task on Rails 5.
I need to "mass" edit a relation between a Project and it's Members
In the UI, I open a popup with a certain list of members (a list of User) and those member have or have not belongs to the Project (check the relations down here).
I need to have the ability to mark some of them (who not belongs to) or unmark others (who belongs to) with checkbox's and "save" the form and create/delete the relations.
For the record I have this models
// project.rb
class Project < ApplicationRecord
has_many :memberships, dependent: :destroy, class_name: 'ProjectMember'
has_many :members, through: :memberships, class_name: 'User', source: :user
end
// project_member.rb
class ProjectMember < ApplicationRecord
belongs_to :user
belongs_to :project
end
// user.rb
class User < ApplicationRecord
has_many :project_members
has_many :projects, through: :project_members
end
I'm kinda new on Rails and I'm really stucked in the way to create the forms (using SimpleForms) and how to edit the relation.
What's the correct approach? I tried to find over the web without success :(
I hope my question it's clear enough :)
I think the helpers you want are included in the gem cocoon : https://github.com/nathanvda/cocoon
Using this gem, your view for the project edition would look like this :
projects/_form
= simple_form_for #project do |f|
-# your project fields...
%h3 Members
#members
= f.simple_fields_for :members do |member|
= render 'member_fields', f: member
.links
= link_to_add_association 'add member', f, :members
= f.submit
projects/_member_fields
.nested-fields
= f.association :user
= link_to_remove_association "remove user from project", f
Edit : I focused on the view part, but before that, in the model, you have to accept nested attributes from forms :
class Project < ApplicationRecord
has_many :memberships, dependent: :destroy, class_name: 'ProjectMember'
has_many :members, through: :memberships, class_name: 'User', source: :user
accepts_nested_attributes_for :members, reject_if: :all_blank, allow_destroy: true
end
There's also a bit of work to do in the controller to permit the nested attributes.
def project_params
params.require(:project).permit(:name, :description, members_attributes: [:id, :user_id, :_destroy])
end

Best approach for creating a polling app in Rails

My models:
class Poll < ActiveRecord::Base
validates_presence_of :title
validates_presence_of :description
has_many :questions, dependent: :destroy
has_many :responses, through: :questions
accepts_nested_attributes_for :questions, reject_if: lambda { |a| a[:title].blank? }, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :poll
has_many :answers, dependent: :destroy
has_many :responses, through: :answers
accepts_nested_attributes_for :answers, reject_if: lambda { |a| a[:content].blank? }, allow_destroy: true
end
class Answer < ActiveRecord::Base
belongs_to :question
has_many :responses
end
class Response < ActiveRecord::Base
belongs_to :answer
end
When you go to /polls/.:id it shows the poll with its corresponding questions and each question with its corresponding answers.
I've been playing around with this answer but I don't know what to do from there. I want whomever I send a poll link (/polls/.:id) to to be able to answer that poll. The method described in the answer linked before creates a form for each question.
I'd check out the documentation on rails views here:
http://guides.rubyonrails.org/action_view_overview.html
There is tons of documentation on how to access and transport data with various form elements.

simple_form association not working as expected

I have a simple_form that is building my form for ResponseSet. A response set is basically collection of responses to a questionnaire (which has_many questions). A question can have many answers. (It's complicated, I know). When attempting to render out the radio buttons for answers to a particular question, none of the radio buttons get selected even though there are answers related to this response. Changing the as: to :check_boxes seems to work correctly. Is this a bug w/ SimpleForm?
= simple_form_for #response_set do |rs|
= rs.simple_fields_for :responses do |r|
- if r.object.question.class <= Question::SingleChoice
= r.association :answers, as: :radio_buttons, collection: r.object.question.answers, label_method: :text, label: false
response_set.rb
class ResponseSet < ActiveRecord::Base
has_many :responses
accepts_nested_attributes_for :responses
end
response.rb
class Response < ActiveRecord::Base
belongs_to :question
has_and_belongs_to_many :answers
belongs_to :response_set
has_many :questions,
through: :answers
accepts_nested_attributes_for :answers
end
questionnaire.rb
class Questionnaire < ActiveRecord::Base
has_many :response_sets
end
answer.rb
class Answer < ActiveRecord::Base
has_many :responses
belongs_to :question
end
question.rb
class Question < ActiveRecord::Base
has_many :responses,
through: :answers
has_many :answers
end
Try adding a :selected option to the association. For example:
= r.association :answers,
as: :radio_buttons,
collection: r.object.question.answers,
label_method: :text,
label: false,
selected: r.object.question.answers.select {|ans| ans.responses.any? }
Also, if you need to build a lot of questionnaires/surveys (especially if they're complicated), I'm a huge fan of the surveyor gem, even though their default CSS is ugly.

Resources