2

group controller:

def show
    @cat = Category.find_by_id(params[:id])
    if @cat.group
        @group = @cat.group
        @members = @cat.group.group_members.all
        @mem = @group.group_members.build
    else
        @cat.build_group
        @cat.save
        @mem = @cat.group.group_members.build
    end
end

def add_member
    @cat = Category.find_by_id(params[:id])
    @group = @cat.group
    @group.group_members.build(member_params)
    if @group.save
        redirect_to group_path

end

view:

- if @members.length > 0
    - @members.each do |member|
        %ul
            %li
                = member.first_name

= simple_form_for @mem, url: member_add_path(@cat.id), html: {:id => 'step_two_form'} do |f|

    = f.label "First name"
    = f.input :first_name, label: false
    = f.label "Last name"
    = f.input :last_name, label: false
    = f.label "Email"
    = f.input :email, label: false
    = f.label "Phone number"
    = f.input :telephone, label: false

    = f.button :button, "Add member"

When I submit this form I can see that a new object is created as there is a new <li> in the source however the object has blank values, regardless of the input.

params (in the group controller):

def member_params
    params.require(:group_member).permit(group_members_attributes: [:first_name, :last_name, :email, :telephone, :relationship, :status])
end

In the terminal I can see that the values I input are being passed but for some reason are not being saved. Here is the terminal output:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"7odxnZzHoyjgF/oougDIVKNR/9RkZOlK3IOpCaUVvpQ=", "group_member"=>{"first_name"=>"name", "last_name"=>"name", "email"=>"[email protected]", "telephone"=>"1234567890"}, "button"=>"", "id"=>"22"}

All help is appreciated, thank you.

EDIT:

group_member.rb:

class GroupMember < ActiveRecord::Base
    belongs_to :group
    attr_accessor :first_name, :last_name, :email, :telephone, :relationship
end
1
  • Can you post your models with associations? Commented Jul 31, 2014 at 14:30

3 Answers 3

5

Extending @DylanMarkow's answer, if you are trying to save first_name, last_name, email, telephone, relationship fields in database then you need to remove the following line from GroupMember model:

attr_accessor :first_name, :last_name, :email, :telephone, :relationship

Due to the attr_accessor, the above mentioned fields are considered as virtual attributes and hence not saved in database.

UPDATE

Can you briefly explain what the purpose of attr_accessor is? I thought it creates a getter and setter methods for the listed attributes?

Yes, attr_accessor is a Ruby method and it creates getter and setter methods for an attribute. When you use attr_accessor within a Rails model, the listed attributes are treated as virtual attributes i.e., there values would be in memory/ accessible only till the instance of the model lives because it is not stored in the database fields (as it is marked as virtual attribute). In a Rails model you don't need to worry about getters and setters of attributes as ActiveRecord would take care of that.

Sign up to request clarification or add additional context in comments.

2 Comments

That did it! Thank you! I'm coming from a Python/Django background and am relatively new to RoR. Can you briefly explain what the purpose of attr_accessor is? I thought it creates a getter and setter methods for the listed attributes?
@apardes Read the UPDATE section in my answer for your reference.
5

Your member_params method doesn't need to specify group_members_attributes as a nested hash, you should just be able to permit the attributes directly (they'll be permitted on whatever you put in the require(...) part):

def member_params
  params.require(:group_member).permit(:first_name, :last_name, :email, :telephone, :relationship, :status)
end

1 Comment

I was under the impression that was necessary since this was in the groups controller and not the group_members controller. Either way, did not solve the issue.
0

combined with @DylanMarkows answer you are saving the group but not necessarily the member

def add_member
  @cat = Category.find_by_id(params[:id])
  @group = @cat.group
  @group_member = @group.group_members.build(member_params)
  if @group_member.save
    redirect_to group_path
  else
    render ...
  end
end

that might help also your method is missing an end and probably an else on save

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.