Ruby on rails 如何获取要发布到Rails的课程id

Ruby on rails 如何获取要发布到Rails的课程id,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试添加帖子,但帖子管理员无法通过params[:id]获取课程id 这是my post controller中的创建方法: def create @coursePost = CoursePost.new(post_params) @coursePost.course_id = params[:id] if (@coursePost.save) redirect_to "/courses" end end def show @posts = CoursePost.all.w

我正在尝试添加帖子,但帖子管理员无法通过
params[:id]
获取课程id

这是my post controller中的创建方法:

def create
 @coursePost = CoursePost.new(post_params)
 @coursePost.course_id = params[:id]
 if (@coursePost.save)
  redirect_to "/courses"
 end
end
def show
 @posts = CoursePost.all.where("course_id = ?", params[:id])
 @newPost = CoursePost.new
end
@coursePost.course_id = params[:course_id]
此表单从各个课程展示页面获取文本字段数据:

<%= form_for(@newPost) do |f| %>
 <div class= "field">
  <%= f.text_field :title %>
 </div>
 <div class= "field">
  <%= f.text_field :content %>
 </div>
 <div class= "actions">
  <%= f.submit "Post!" %>
 </div>
<% end %>

您正在为课程设置
course\u id
,就像您正在发送的id参数一样,但在您的表单中,您没有发送任何id,只发送
标题
内容

因此,您可以尝试添加带有任何输入的
课程id
,以便能够接收它,并且控制器中的将其分配给
@coursePost
,作为
参数[:课程id]
,如:

在你看来:

<%= form_for(@newPost) do |f| %>
  <div class= "field">
    <%= f.text_field :course_id %>
  </div>
  <div class= "field">
    <%= f.text_field :title %>
  </div>
  ...

您正在为课程设置
course\u id
,就像您正在发送的id参数一样,但在您的表单中,您没有发送任何id,只发送
标题
内容

因此,您可以尝试添加带有任何输入的
课程id
,以便能够接收它,并且控制器中的将其分配给
@coursePost
,作为
参数[:课程id]
,如:

在你看来:

<%= form_for(@newPost) do |f| %>
  <div class= "field">
    <%= f.text_field :course_id %>
  </div>
  <div class= "field">
    <%= f.text_field :title %>
  </div>
  ...

最近我做了一个类似的项目。以下是我的代码供您参考

def create
  @course = Course.find(params[:course_id])
  @coursePost = CoursePost.new(post_params)
  @coursePost.course = @course
  @coursePost.user = current_user
  if @coursePost.save
     redirect_to course_path(@course)
  else
     render :new
  end
end

private
def post_params
    params.require(:coursePost).permit(:content)
end
以下是课程控制器中的显示方法

def show
  @course = Course.find(params[:id])
  @coursePosts = @course.coursePosts
end

最近我做了一个类似的项目。以下是我的代码供您参考

def create
  @course = Course.find(params[:course_id])
  @coursePost = CoursePost.new(post_params)
  @coursePost.course = @course
  @coursePost.user = current_user
  if @coursePost.save
     redirect_to course_path(@course)
  else
     render :new
  end
end

private
def post_params
    params.require(:coursePost).permit(:content)
end
以下是课程控制器中的显示方法

def show
  @course = Course.find(params[:id])
  @coursePosts = @course.coursePosts
end

如果您遵循Rails的建议,这将为您处理:

# routes
resources :courses do
  resources :posts
end

# posts controller
def new
  @course = Course.find(params[:course_id]
  @post = @course.posts.new
end

def create
  @course = Course.find(params[:course_id])
  @post = @course.posts.new(post_params.merge(user_id: current_user.id)
  # ...
end

# form
form_for [@course, @post] do |f|
  # ...
end

如果您遵循Rails的建议,这将为您处理:

# routes
resources :courses do
  resources :posts
end

# posts controller
def new
  @course = Course.find(params[:course_id]
  @post = @course.posts.new
end

def create
  @course = Course.find(params[:course_id])
  @post = @course.posts.new(post_params.merge(user_id: current_user.id)
  # ...
end

# form
form_for [@course, @post] do |f|
  # ...
end

非常感谢您的快速响应,感谢您,我成功地修复了它。谢谢,但我这样做了,它将其添加到当前课程中,然后刚刚做了params[:course_id]太好了,很高兴能提供帮助。非常感谢您的快速响应,多亏了你,我成功地修复了它。谢谢,但我这样做了,所以它将它添加到当前课程中,然后只是做了params[:course_id],太好了,很高兴能帮上忙。