我想創建一個應用程序,用戶可以創建一個主題,然後其他人可以創建主題。我嵌套我的資源,我的routes.rb:嵌套資源創建無法找到沒有ID的主題
MyPedia2::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :topics, only: [:show, :create, :destroy]
resources :posts
resources :topics do
resources :posts, only: [:create, :show, :new]
end
在我演講的題目顯示頁面,我想告訴topic.title和sended郵電post.form.html.erb。 一切正常接受,當我創建一個帖子,我得到的錯誤
ActiveRecord::RecordNotFound in PostsController#create
Couldn't find Topic without an ID..
這是我posts_controller.rb:
class PostsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def new
@topic= Topic.find_by_id(params[:id])
@post = @topic.posts.build(params[:post])
end
def show
@topic = Topic.find(params[:id])
@posts = @topic.posts.paginate(page: params[:page])
end
def create
@topic = Topic.find(params[:id])
@post = @topic.posts.build(params[:post])
@post.topic = @topic
if @post.save
flash[:success] = "Konu oluşturuldu!"
redirect_to :back
else
render 'static_pages/home'
end
end
def destroy
@post.destroy
redirect_to root_path
end
private
def correct_user
@post = current_user.posts.find_by_id(params[:id])
redirect_to root_path if @post.nil?
end
end
和_post_form.html.erb:
<%= form_for @new_post do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "yorumunuzu girin..." %>
</div>
<%= f.submit "Gönder", class: "btn btn-large btn-primary" %>
<% end %>