2017-11-08 1 views
0

그래서 간단한 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 
+0

'루트 경로 '란 무엇입니까? – Gabbar

+1

'id = 1' 또는'Listing.find (1)'을 사용하여 레코드가 존재하는지 확인하십시오. – Gabbar

+0

URLGenerationError - '경로 일치 {: action => "show", : controller => "listings", : id => nil}, 필수 키 누락 : [: id]. 제출을 클릭하면 내 ID가 올바르게 표시되지 않습니까? 추가 할 필요가있는 다른 코드가 있습니까?이 양식을 작성한 후에 –

답변

-1

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 

로 수행하고 플래시에서 오류 메시지를 확인

0

그것은 당신이 목록을 만들려고 할 때, 당신은 유효성 검사 오류의 몇 가지 유형을 타격하고있을 가능성이 높습니다 및 작성이 롤백되고 . 이것에 대해 유익한 오류가 표시하기 위해 교체하여 작성 조치를 업데이트합니다

@listing.save 

@listing.save! 

(당신이 무엇을) 자동으로 실패하고 단지 잘못된 기록에 대해 false를 반환 이전을

와 함께. 후자 (강타 포함)는 유효하지 않은 레코드에 대한 예외와 함께 실패합니다.

관련 문제