Ruby on rails post_属性的不允许参数
因此,我试图让我的主题控制器创建一个带有初始帖子的新主题。我的新主题视图如下所示Ruby on rails post_属性的不允许参数,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,因此,我试图让我的主题控制器创建一个带有初始帖子的新主题。我的新主题视图如下所示 <% @title = "New Topic" %> <h1>New Topic</h1> <%= form_for [@topic.forum, @topic] do |f| %> <%= render "topic_form", f: f %> <%= f.submit "Create Topic", class: "btn btn-
<% @title = "New Topic" %>
<h1>New Topic</h1>
<%= form_for [@topic.forum, @topic] do |f| %>
<%= render "topic_form", f: f %>
<%= f.submit "Create Topic", class: "btn btn-primary" %>
<% end %>
具有强参数的东西
但无论我做什么,它都会断裂。开发日志说有
Unpermitted parameters: posts_attributes
我在网上搜索了无数个小时,但没有成功。任何人都知道如何解决这个问题。现在,当我单击主题“新建”视图中的“提交”按钮时,它会提交标题,但您输入的内容会丢失,当我创建新帖子时,它会正常工作,并打印出用户输入的内容。因此,它只有在创建新主题时才会中断,而唯一中断的部分是内容部分。您似乎正在提交帖子\u属性,请在帖子后添加注释。您定义的参数称为post_attributes。我认为这实际上解决了这个问题,但快速提问它中断了,因为我的视图需要用户id才能访问用户名。为什么我在做params.require:topic.permit:title,:post\u attributes=>[:id,:user\u id=>current\u user.id,:topic\u id,:content]时不起作用?我需要以某种方式将用户id链接到最初的帖子。有什么想法吗?我想我已经知道了。在create方法中,我刚刚做了一个janky设置,其中@topic.posts[length].user\u id=current\u user.id。如果你对如何解决这个问题有更好的想法,那就请便。
class Topic < ActiveRecord::Base
belongs_to :forum
belongs_to :user
has_many :posts, :dependent => :destroy
validates :title, presence: true
accepts_nested_attributes_for :posts, allow_destroy: true
end
def new
forum = Forum.find(params[:forum_id])
@topic = forum.topics.build
post = @topic.posts.build
end
def create
forum = Forum.find(params[:forum_id])
@topic = forum.topics.build(topic_params)
@topic.last_poster_id = current_user.id
@topic.last_post_at = Time.now
@topic.user_id = current_user.id
if @topic.save then
flash[:success] = "Topic Created!"
redirect_to @topic
else
render 'new'
end
end
def topic_params
params.require(:topic).permit(:title, :post_attributes => [:id, :topic_id, :content])
end
Unpermitted parameters: posts_attributes