Я пытаюсь создать очень простое приложение, похожее на форум, где пользователи могут создавать темы и отвечать на существующие темы.
Создание темы работает нормально, но я могу отобразить форму ответа, однако, действие Создать ответ не работает должным образом.У меня нет никаких ошибок, просто redirects_to topics_path
.
Это следующий учебник, поэтому код не мой.Кто-нибудь может определить очевидную причину этого?Любая помощь высоко ценится!
replies_controller.rb
def create
@topic = Topic.find(params[:topic_id])
@reply = @topic.replies.create(params[:reply].permit(:reply))
@reply.user_id = current_user.id if current_user
@reply.save
if @reply.save
redirect_to topic_path(@topic)
else
flash[:notice] = "Error."
redirect_to topics_path
end
end
reply.rb
class Reply < ApplicationRecord
belongs_to :post
belongs_to :user
end
ответов / _form.html.erb
<%= form_for [@topic, @topic.replies.create] do |f| %>
<%= f.label :reply %>
<%= f.text_area :reply, class: "textarea", rows: "10" %>
<%= f.submit class: "button is-primary" %>
<% end %>
topic.rb
class Topic < ApplicationRecord
belongs_to :user
has_many :replies
end
schema.rb
create_table "topics", force: :cascade do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
create_table "replies", force: :cascade do |t|
t.text "reply"
t.bigint "topic_id"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["topic_id"], name: "index_replies_on_topic_id"
t.index ["user_id"], name: "index_replies_on_user_id"
end