【发布时间】:2016-03-03 04:46:23
【问题描述】:
我有一个嵌套在课程资源中的 cmets 资源:
路线.rb:
我想在任何课程的显示页面中显示表格以发表评论。
cmets_controller.rb:
class CommentsController < ApplicationController
before_action :require_user
before_action :set_course, only: [:new]
def new
@comment = Comment.new
end
def create
@comment = Comment.new(comment_params)
@comment.course = Course.find(params[:course_id])
@comment.user = current_user
if @comment.save
flash[:success] = "Comment Posted"
redirect_to course_path(@comment.course)
else
flash[:error] = "Comment can't posted"
redirect_to course_path(@comment.course)
end
end
private
def set_course
@course = Course.find(params[:course_id])
end
def comment_params
params.require(:comment).permit(:body ,:rating, :course_id, :user_id)
end
end
我已将@comment = Comment.new 放入我的课程控制器的显示操作中。
我在视图中使用的形式是:
course_cmets_path(@course, @comment) ) 做 |f| %>
<div class="rrating" style="width:140px;">
<%= f.radio_button(:rating, 1) %>
<%= f.label(:rating_1, "1") %>
<%= f.radio_button(:rating, 2) %>
<%= f.label(:rating_2, "2") %>
<%= f.radio_button(:rating, 3) %>
<%= f.label(:rating_3, "3") %>
<%= f.radio_button(:rating, 4) %>
<%= f.label(:rating_4, "4") %>
<%= f.radio_button(:rating, 5) %>
<%= f.label(:rating_5, "5") %>
</div>
<%= f.text_field :body, :class => "form-control", id: "exampleInputEmail1", placeholder: "Add a comment" %>
</fieldset>
<%= f.submit("Post", class: 'btn btn-success') %>
<% end %>
</form>
当我点击提交时,获取请求会发送到我当前所在的同一页面(所有参数都在 url 中)。
我尝试将表单放在 cmets 的单独新页面中,它仍然不起作用。
我做错了什么?
【问题讨论】:
-
试试form_for这样,
<%= form_for([@course, @comment],:url => course_comments_path(@course, @comment),method: "post" ) do |f| %>把Url作为第二个参数 -
任何人都有任何解决方案......我仍然无法让它工作......
标签: ruby-on-rails ruby forms