Using Strong Attributes with Rails 4 and has_one relationship

Asked

Viewed 109 times

1

I have a relationship has_one similar to its where a :person has a :address.

It turns out that the relationship is generated but the street that I put in input :street (address attribute) is not saved. I have already put as your model above and returns error ADDRESS IS NOT DEFINED.

If you could give me a hand, I’d be grateful.

the lab is on github: https://github.com/vanessasoutoc/labHasOne

address.rb
class Address < ActiveRecord::Base
    belongs_to :person
end

person.rb
class Person < ActiveRecord::Base
    has_one :address
    accepts_nested_attributes_for :address
end

people_controller.rb
class PeopleController < ApplicationController
    before_action :set_person, only: [:show, :edit, :update, :destroy]

    def address
        @address
    end

    def addresses_atributes=(attributes)

    end

    # GET /people
    # GET /people.json
    def index
        @people = Person.all
    end

    # GET /people/1
    # GET /people/1.json
    def show
    end

    # GET /people/new
    def new
        @person = Person.new
        @person.build_address
    end

    # GET /people/1/edit
    def edit
    end

    # POST /people
    # POST /people.json
    def create
        @person = Person.new(person_params)
        @person.build_address
        respond_to do |format|
            if @person.save
                format.html { redirect_to @person, notice: 'Person was successfully created.' }
                format.json { render :show, status: :created, location: @person }
            else
                format.html { render :new }
                format.json { render json: @person.errors, status: :unprocessable_entity }
            end
        end
    end

    # PATCH/PUT /people/1
    # PATCH/PUT /people/1.json
    def update
        respond_to do |format|
            if @person.update(person_params)
                format.html { redirect_to @person, notice: 'Person was successfully updated.' }
                format.json { render :show, status: :ok, location: @person }
            else
                format.html { render :edit }
                format.json { render json: @person.errors, status: :unprocessable_entity }
            end
        end
    end

    # DELETE /people/1
    # DELETE /people/1.json
    def destroy
        @person.destroy
        respond_to do |format|
            format.html { redirect_to people_url, notice: 'Person was successfully destroyed.' }
            format.json { head :no_content }
        end
    end

    private
    # Use callbacks to share common setup or constraints between actions.
    def set_person
        @person = Person.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    private
    def person_params
        params.require(:person).permit(:name, :cpf, addresses_atributes: [:street, :zip_code])
    end

end

#/people/_form.html.erb
<##%= form_for(@person) do |person_form| %>




  <div class="field">
    <%#= person_form.label :name %><br>
    <%#= person_form.text_field :name %>
  </div>
  <div class="field">
    <%#= person_form.label :cpf %><br>
    <%#= person_form.text_field :cpf %>
  </div>
  <%#= person_form.fields_for :address do |address_form| %>
  <div class="field">
      <%#= address_form.label :street %>
      <%#= address_form.text_field :street %>
  </div>
  <div class="field">
      <%#= address_form.label :zip_code %>
      <%#= address_form.text_field :zip_code %>
  </div>
  <% end %>
  <div class="actions">
    <%#= person_form.submit %>
  </div>
<#% end %>
  • Can you put the class that defines address? Also, try to give more details. It is very difficult to find the problem, since you just passed the Github project to this...

  • 2

    Welcome to Sopt, make one tour to better understand how our community works. Explain the problem better, and if possible include a example of code that reproduces what is happening, because your question is too wide. See Help Center How to Ask.

  • The following happens I have two models, person and address. When I create a person he has to generate the link between person and address, and he generates the relationship... Entering the address (:street) and other information that I enter through the fields_for are not being saved in the database...

  • I edited the question. Thank you for helping..

1 answer

1


There are two problems in your code. The first is the name of the nested parameter of the model address down in the person_params.

people_controller.Rb

def person_params
  params.require(:person).permit(:name, :cpf, address_attributes: [:street, :zip_code])
end

And the other problem is the method create, you must remove the line @person.build_address, because it is not necessary when you are already creating a record with the data submitted by the user, use the build_address only in the method new.


Remember that you should also look at the log of your app to discover this type of problem, see the log when I submit the form to create a person:

tarted POST "/people" for 127.0.0.1 at 2015-01-20 11:02:06 -0200
Processing by PeopleController#create as HTML
  Parameters: {
    "utf8"=>"✓",
    "authenticity_token"=>"rcEOWTBmMgcq3tOrNUiDForZnlB6tI4MGtg9mWurSDQ=",
    "person"=>{
      "name"=>"Person",
      "cpf"=>"0000000000",
      "address_attributes" => { "street"=>"Street name"}
    },
    "commit"=>"Create Person"
   }

There was the answer to the problem of the wrong name in person_params.

  • Thank you very much. I’ve tried it here and it worked.. Tava almost gave up. Silly mistakes.. Thank you very much.

  • I’m glad you helped! Then read here to help stackoverflow http://answall.com/help/someone-answers =)

  • 1

    People don’t even vote when they get help, they take my vote @Peoplee

Browser other questions tagged

You are not signed in. Login or sign up in order to post.