Ruby on Rails: Nested Attributes, belongs_to relation -


I have a user unit that has the current location field (city and country). For the old information, I have created the named unit, which is another user.

I am not completely convinced that I should be kept in the user's model "is_on" or "attaito", but what I have read. If I wanted to put the foreign key of the place I "should_to" I want to be able to edit the user's current location while editing a user. That's why I'm using nested features. But when I edit the user, I connect it every time without any new location, it connects the user who was edited. Could you help me?

My code is as follows:

  # User Model class user & lt; ActiveRecord :: Base ## Relationships are inserted: Current_space ,: class_name = & gt; Accepts 'Location' for the nominated_Authority: Current_space location end # location model class space & lt; ActiveRecord :: base # relationship is_maya: User # ends of # form_edit.haml - form_edit.fields_for: current_location do | Location_form | = Location_form.label: Location, "Current Location" = location_form.text_field: Location # For the user to make the efesis helper #test attributes and setup user (user) returning. You U.build_current_location if u.current_location.nil? End Controller (added after edit) Def update @user = @current_user if @ user.update_attributes (Perm [: User]) Flash [: notice] = "Account Update!" Redirect_to account_url Other render: Action = & gt; : End of End Edit  

The exact problem is facing, as Others have told that your controller is not receiving the location ID as it should be. I think the location ID is being passed through the wrong parameters. Unfortunately, a location ID is not present on a new record, so it is not possible in the form.

Used to accept your problem in relation to a relationship, __ nested_attributes_for is used. Behavior is not clearly defined, it appears to be a documented bug, therefore acceptable_nested_tatabases must be on one or have multiple sides of the relationship.

Here are some possible solutions:

  1. Move accepted-nested_data to the location model and make your form in another way.

      -form_for @location do | Location_form | ... = location_form.fields_for @user do | User_form | ....  

    Unfortunately this information is not a logical way to present. And makes the right user difficult.

  2. Use an insert model, and one through one: through the relationship.

    I honestly do not know how to work well with the relationship; _An_TENT_DATA_Now works, but with the linking of the records it will certainly solve your problem.

  3. Accept the ignore_redged_data and handle the old fashioned way in your controller.

    Actually, it does provide some simple feature for acceptance_Act_At_Application_, just do not give it to update_attributes / create statement.

      def update @user = @current_user complete = incorrect_place_perm = parameter [: user] .delete (: Current_location_attributes) User.transaction do @location = location.find_core_boot_b_id (location_update) @user.update (Paras [: User]) @ UserRunter_Location = @post @ user. Save! Complete Flash Complete = True End [: Notice] = "Account Update!" Redirect_to account_url Other render: Action = & gt; : End of End  

Fields currently reside in an id field; status hashes automatically, if it is not creating a new space, find_or_create_by_id Id need entry in the hash to work for it. If this ID is not in the database, it will correctly create with the auto-generated ID. If you are creating a new space then you have to add it. This is the easiest to add to the form with = location_form.hidden_field: id, 0, as long as the current \ _location.new \ _record?

However, you want to cut duplicate space creation, and replace the location.find_OR_create_b_id line in location.find_OR_ERE _________ location. This will also cut any failures from failing assumptions.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

kernel - How to create a "Block device" in Windows -