2015-02-05 2 views
0

일부 데이터를 db 및 ok로 제출하려고 시도하지만 해당 데이터를 검색하려고 할 때 ID없이 기사를 찾을 수 없습니다. .ils 4.0.1. 나는 루비 2.0.0와 라ArticlesController # ActiveRecord :: RecordNotFound ID없이 기사를 찾을 수 없습니다

def show 
@article=Article.find(params[:id]) 

the ** contain error. 

class ArticlesController < ApplicationController 
    def new 
    end 
    def create 
    @article = Article.new(article_params) 
    redirect_to @article 
    @article.save 
    end 

def show 
    @article=Article.find(params[:id]) 
    end 
    private 
    def article_params 

    params.require(:article).permit(:title, :full_name, :email, :phone_number, :message) 
    end 

    end 

기사/show.html.erb를 사용하고

<p> 
<strong>Title:</strong> 
<%= @article.title %> 
</p> 

<p> 
<strong>Full Name:</strong> 
<%= @article.full_name %> 
</p> 

<p> 
<strong>Email:</strong> 
    <%= @article.email %> 
    </p> 

<p> 
    <strong>Phone Number:</strong> 
    <%= @article.phone_number %> 
    </p> 
    <p> 
    <strong>Message:</strong> 
    <%= @article.message %> 
    </p> 

기사/new.html.erb

<h1>New Articles</h1> 

<%= form_for :article, url: articles_path do |f| %> 
    <p> 
    <%= f.label :title %> 
    <%= f.text_field :title %> 
    <p> 
    <%= f.label :full_name %> 
    <%= f.text_field :full_name %> 
    </p> 
    <%= f.label :email %> 
    <%= f.text_field :email %> 
    <p> 
    <%= f.label :phone_number %> 
    <%= f.text_field :phone_number %> 
    </p> 
    <%= f.label :message %> 
    <%= f.text_field :message %> 
    <p> 
    <%= f.submit :send_message %> 
    </p> 

    <% end %> 
+0

다시 리디렉션 오류. –

+0

ArticleController에서 NoMethodError는 #

답변

2

AC 이전에 리디렉션 중입니다. 개인적으로 문서를 저장하는 중.

이 :

def create 
    @article = Article.new(article_params) 
    redirect_to @article 
    @article.save 
end 

은 다음과 같아야합니다

def create 
    @article = Article.new(article_params) 
    @article.save 
    redirect_to @article  
end 

그리고 당신은뿐만 아니라 처리 일부 오류를 추가하려면 : 나는 버튼의 쇼를 저장 누르면

def create 
    @article = Article.new(article_params) 
    if @article.save 
    redirect_to @article 
    else 
    render :new 
end