0

I'm trying to save a form_for with nested resources that inputs data into a model that belongs to 2 other models and has 2 foreign keys: User_id and listing_id.

The problem is that only the user_id gets saved despite the fact that both ids are being created ,as shown in the console, and the listing_id is passed with the correct value.

I might be missing something obvious but i'm fairly new to Rails. Help is much appreciated. Thanks.

Application model:

    belongs_to :user
    belongs_to :listing

Listing model:

    has_many :applications

User model

    has_many :applications

Controller:

    class ApplicationsController < ApplicationController
    before_action :authenticate_user! 


    def index

    end

    def update
      @application = current_user.applications.create  
    end


    def new 
      @application = Application.new
      @listing = Listing.find(params[:listing_id])

    end

    def create
      @application =     current_user.applications.create(application_params)
      redirect_to listing_applications_path
    end

    def your_applications
      @apps = current_user.applications

    end

    private
      def application_params
        params.require(:application).permit(:listing_id, :st_address, :unit, :city, :state, :move_in,:first_name, :last_name, :phone_numer, :pets, :current_address, :previous_landlord_name, :previous_landlord_phone, :company, :company_address, :company_phone, :is_40X, :annual_income)
      end
  end

View:

      <%= form_for [:listing, @application] do |f| %>

routes.rb

    resources :listings do
      resources :applications
    end

This is what I get in the controller- https://i.sstatic.net/e52T0.png. The listing_id is passed already as a parameter but gets lost when saving the form.

Processing by ApplicationsController#new as HTML Parameters: {"listing_id"=>"13"}

Processing by ApplicationsController#create as HTML Parameters: {........, "listing_id"=>"13"}

User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 11]] (0.1ms) begin transaction SQL (0.4ms) INSERT INTO "applications" (.......) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [....., ["user_id", 11], ["created_at", ....], ["updated_at", ....]]

Listing Load (0.1ms) SELECT "listings".* FROM "listings" WHERE "listings"."id" = ? LIMIT 1 [["id", 13]]

It seems like the listing_id is being passed but not inserted. Why is that?

3
  • How are you passing listing_id to the create action? What does the complete form look like? Commented Mar 20, 2016 at 5:43
  • Maybe i'm wrong but it seems like the listing_id is passed from the url but not inserted based on the message in the console. I added the console messages above. I also included this line in the "new" action: @listing = Listing.find(params[:listing_id]) but still I have the same problem. Commented Mar 20, 2016 at 15:11
  • I found a solution which is to create a hidden field in the view that will be pre populated with the listing_id. In that case it works if I add the following to the view: <%= f.text_field :listing_id, value: params[:listing_id], class: "form-control hidden"%>. But it seems not elegant and I wish I can find a better solution. I still don't understand why the listing_id is not saved without adding that line. Commented Mar 20, 2016 at 17:48

2 Answers 2

0

Your 'new' action shouldn't be creating anything. It should be:

@application = Application.new

And your "update" action should not be creating a new record but updating the record that was just edited. So that would become:

@application = Application.find(params[:id])
@application.update_attributes(application_params)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer but I still get the same result. The listing_id is not saving.
0

A possible solution is to add a hidden field in the view that will be pre populated with the listing_id.

It works if this is added to to the view:

    <%= f.text_field :listing_id, value: params[:listing_id], class: "form-control hidden"%>.

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.