我是 Rails 新手,正在尝试创建一个论坛。论坛有很多话题,话题属于一个论坛,有很多微博,微博既属于话题又属于用户。但是,无论我尝试什么,都不会创建帖子。目前,当我尝试发布时,我收到路由错误“没有路由匹配 [GET]“/topics””
我的 routes.rb 文件:
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :forums, only: [:index, :show]
resources :topics, only: [:show]
_micropost_form.html.erb
<%= form_for(@micropost) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.hidden_field :topic_id, value: @topic.id %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.text_field :summary, placeholder: "One-line summary..." %>
<%= f.text_area :content, placeholder: "Compose a new post..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
<% end %>
microposts_controller.rb
class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
#@topic = Topic.find_by_id(params[:topic_id])
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Your solution has been posted!"
redirect_to topic_path(@topic)
else
redirect_to topic_path(@topic)
end
end
def destroy
@micropost.destroy
redirect_to root_url
end
private
def micropost_params
params.require(:micropost).permit(:summary, :content, :user_id)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
如您所见,我在创建函数中注释掉了第一行,因为我尝试根据微博与主题的关系进行发布,但无济于事。提前感谢,如果我发布更多代码,请告诉我是否有帮助!