2017-11-08 2 views
0

So versuche ich zu lernen, wie eine einfache Ruby on Rails-Anwendung zu machen. Ich erstelle ein Feld, wo der Benutzer Daten eingeben kann. Beim Senden (oder Erstellen) werden diese Daten gespeichert und der Benutzer kehrt dann zur Startseite zurück. Also habe ich localhost: 3000/listings/new, um das Modell zu erstellen, und dann versuche ich, localhost zu erreichen: 3000/listings/1, um das neu erstellte Modell zu sehen und bin mit einem Datensatz nicht gefunden Fehler aufgetreten. Ich bin nicht sicher, warum das sein könnte, und ich habe ein paar Stunden ohne Ergebnisse gesucht.Datensatz nicht gefunden Fehler in Rails Webapp

listings_controller.rb

class ListingsController < ApplicationController 

    def new 
    @listing = Listing.new #calls on new method in listing model 
    end 

    def create 
    @listing = Listing.new(listing_params) 
    @listing.save 
    redirect_to root_path 
    end 

    def show 
    @listing = Listing.find(params[:id]) 
    end 

    private 
    def listing_params 
    params.require(:listing).permit(:title, :description, :city, :state, :zipcode) 
    end 

end 

new.html.erb und show.html.erb (falls erforderlich)

<div class="topbar"> 

</div> 
<div class="container"> 
    <div id="contact-area"> 

    <%= form_for @listing do |f| %> 
    <!-- taken from schema.rb --> 
    <%= f.label :title %> 
    <%= f.text_field :title %> <!-- use text field when body is just 1 line --> 

    <%= f.label :description %> 
    <%= f.text_area :description %> <!-- more for paragraphs --> 

    <%= f.label :city %> 
    <%= f.text_field :city %> 

    <%= f.label :state %> 
    <%= f.text_field :state %> 

    <%= f.label :zipcode %> 
    <%= f.text_field :zipcode, class: "zip-width", maxlength: "5" %> 

    <%= f.submit class: "create-button"%> 

    <% end %> 
    </div> 
</div> 

UND

<div class="topbar"> 
    <div class="container"> 
    <div class="vertical-center"> 
     <%= link_to 'home', root_path %> > jobs > accounting 
    </div> 
    </div> 
</div> 
<div class="container"> 
    <div> 
    <button type="button">reply</button> 
    posted <%= time_ago_in_words(@listing.created_at) %> 
    <h1 class="listing-header"><%= @listing.title %></h1> 
    <div class="box"> 
     <p>test</p> 
    </div> 
    <p><%= @listing.description%></p> 
    </div> 
    <footer> 
    <p>post id: <%= @listing.id%></p> 
    <p>posted <%= time_ago_in_words(@listing.created_at) %></p> 
    </footer> 
</div> 

routes.rb

Rails.application.routes.draw do 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 

    #NOTE USE rake routes TO SEE ALL ROUTES 

    #Creates the CRUD actions for categories 
    resources :categories do 
    resources :subcategories #Creates CRUD actions for subcategories 
    end 

    resources :listings 

    root 'categories#index' #first page that we land on -- homepage 

    #matching paths to pages controller 
    match '/help', to: 'pages#help', via: :get 
    match '/scams', to: 'pages#scams', via: :get 
    match '/safety', to: 'pages#safety', via: :get 
    match '/terms', to: 'pages#terms', via: :get 
    match '/privacy', to: 'pages#privacy', via: :get 
    match '/about', to: 'pages#about', via: :get 
    match '/contact', to: 'pages#contact', via: :get 
end 
+0

was ist dein 'root_path'? – Gabbar

+1

check on rails console wenn Datensatz existiert mit 'id = 1' oder' Listing.find (1) '? – Gabbar

+0

Ich änderte dies und erhielt einen URLGenerationError - 'Keine Route stimmt überein {: action =>" show ",: controller =>" Listings ",: id => nil}, fehlende erforderliche Schlüssel: [: id].' Why ist meine id gleich nachdem ich auf submit geklickt habe? Gibt es irgendeinen anderen Code, den ich hinzufügen muss, damit Sie mir helfen können, dies herauszufinden –

Antwort

-1

Machen Sie es so

def create 
    @listing = Listing.new(listing_params) 
    if @listing.save 
     flash[:notice] = "Listing created successfully." 
     redirect_to listing_path(@listing) 
    else 
     flash[:error] = @listing.errors.full_messages 
     render 'new' 
    end 
    end 

und überprüfen Sie die Fehlermeldung in Flash

0

Es ist wahrscheinlich, dass, wenn Sie versuchen, den Eintrag zu erstellen, werden Sie irgendeine Art von Fehler bei der Überprüfung schlagen und die Schaffung zurück wird gerollt . Um einen informativen Fehler, um dies zu sehen, aktualisieren Sie Ihre Aktion erstellen, indem Sie ersetzen:

@listing.save 

mit

@listing.save! 

Der ehemalige (was Sie haben) nicht still und gibt nur falsch für einen ungültigen Datensatz. Letzteres (mit dem Knall) schlägt mit einer Ausnahme für einen ungültigen Datensatz fehl.