我的应用程序有一些问题,我有帖子,当我为帖子创建新回复时发布 has_many 回复,而不是添加到数据库'responce' post_id my routes.rb
resources :categories do
resources :posts
end
resources :posts do
resources :responces
end
控制器
class ResponcesController < ApplicationController
def new
@post = Post.find(params[:post_id])
@responce = @post.responces.new(post_id:params[:post_id])
end
def create
@responce = current_user.responces.build(responce_params)
@post = Post.find(params[:post_id])
if @responce.save
flash[:success] = "Вы откликнулись на задание"
redirect_to post_path @post
else
render 'new'
end
end
def show
end
private
def responce_params
params.require(:responce).permit(:price, :comment, :post_id)
end
end
看法
<%= form_for([@post, @responce]) do |f| %>
<%= f.text_area :price %>
<%= f.submit "GO", class: "btn btn-large btn-primary" %>
<% end %>
但如果添加到视图中
<%= f.collection_select :post_id, Post.all, :id, :name %>
rails 创建 post_id 到数据库
帮助