그래서 간단한 Ruby on Rails 애플리케이션을 작성하는 방법을 배우려고합니다. 나는 사용자가 데이터를 입력 할 수있는 필드를 만들고있다. 제출 (또는 생성)시이 데이터가 저장되고 사용자가 홈 페이지로 돌아갑니다. 그래서 나는 localhost : 3000/listings/new 모델을 만들었고 localhost : 3000/listings/1에 새로 생성 된 모델을 보려고 시도했고 Record Not Found 오류를 만났다. 왜 이런 일이 있을지 모르겠다. 결과가없는 몇 시간 동안 기다렸다.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 show.html.erb 및 (필요한 경우)
<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>
AND
<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
'루트 경로 '란 무엇입니까? – Gabbar
'id = 1' 또는'Listing.find (1)'을 사용하여 레코드가 존재하는지 확인하십시오. – Gabbar
URLGenerationError - '경로 일치 {: action => "show", : controller => "listings", : id => nil}, 필수 키 누락 : [: id]. 제출을 클릭하면 내 ID가 올바르게 표시되지 않습니까? 추가 할 필요가있는 다른 코드가 있습니까?이 양식을 작성한 후에 –