0

i'm playing around trying to create a website where people can upload their property. i've created a new model and controller where people can do this. The data is successfully saved to the database, but it won't display on the show page. All of the fields return blank.

Started POST "/investments" for 127.0.0.1 at 2018-02-16 11:17:37 +1000
(6.7ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" 
ORDER BY "schema_migrations"."version" ASC
Processing by InvestmentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"r2WOptmoB7Icoal2SHVBTjyhiaLBKoBMydxIyLbiOr6lTs6UVvs0fvuj3TS2whDpLYouGYkWzP1AEg/GghbJAQ==", "investment"=>{"title"=>"test", "description"=>"test", "propertytype"=>"test", "bedrooms"=>"test", "carpark"=>"test", "landsize"=>"test", "equity"=>"test", "cashflow"=>"test", "rating"=>"test"}, "commit"=>"Save Investment"}

show.html.erb

  <strong>Title:</strong>
  <%= @investment.title %>
  <%= @investment.excerpt %>
  <%= @investment.description %>
  <%= @investment.propertytype %>
  <%= @investment.bedrooms %>
  <%= @investment.carpark %>
  <%= @investment.landsize %>
  <%= @investment.equity %>
  <%= @investment.cashflow %>
  <%= @investment.rating %>

investments_controller.erb

class InvestmentsController < ApplicationController

    def show
    @investment = Investment.new(params.permit(:title, :excerpt, :description, :propertytype, :bedrooms, :carpark, :landsize, :equity, :cashflow, :rating))
    end

    def new
    end

    def create
        @investment = Investment.new

        @investment.save
        redirect_to @investment
    end


private
  def investment_params
    params.permit(:title, :excerpt, :description, :propertytype, :bedrooms, :carpark, :landsize, :equity, :cashflow, :rating)
  end
end

routes.rb

 Rails.application.routes.draw do
  mount Ckeditor::Engine => '/ckeditor'
    get 'pages/index' => 'high_voltage/pages#show', id: 'index'
    root :to => 'high_voltage/pages#show', id: 'index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  resources :investments
end

here's the final result^

2 Answers 2

2

You should find the Investment object by its ID. Currently you're attempting to instantiate a new object on Show. For example:

@investment = Investment.find(params[:id])

Instead of

@investment = Investment.new(params.permit(:title, :excerpt, :description, :propertytype, :bedrooms, :carpark, :landsize, :equity, :cashflow, :rating))

Also, your create method is saving a blank Investment object, it should be changed to something like this:

@investment = Investment.new(investment_params)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! i just tried those.. still same result. I also restarted the rails server. Any other ideas what could be causing this issue? i also tried to create another model and controller and it did the same thing. I'm running postgres as the database
It also depends on how your #new view is configured. Just to confirm your show route is working as expected, I'd create an Investment object in the console and access it directly via the URL. If it is still not displaying the data, try [email protected], to see what you're actually getting back, within the view. It may provide a hint as to what is going wrong.
1

Are you sure the Investment is saved? Besides the changes that Anthony correctly stated:

@investment = Investment.find(params[:id]) # In show.

@investment = Investment.new(investment_params) # In create

you must change the method investment_params

def investment_params
  params.require(:investment).permit(:title, :excerpt, :description, :propertytype, :bedrooms, :carpark, :landsize, :equity, :cashflow, :rating)
end

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.