我的 form_for 没有重定向到我指定的操作。我真的不知道问题出在哪里。
我的查看详情:
<!-- Comments Form -->
<div class="well">
<h4>Comment:</h4>
<form class="comment-form">
<div class="form-group">
<%= form_for(:comments, :url => {:action => 'post_comment'}) do |f| %>
<%= f.text_area(:content, :rows => "3", :class => "form-control new-comment") %>
<%= f.hidden_field :book_id, :value => @comments[:book_id] %>
<%= f.hidden_field :user_id, :value => session[:user_id] %>
<%= submit_tag("Send") %>
<% end %>
</div>
</form>
</div>
我的 Controller :
def detailedbook
@book = Book.find(params[:id])
@comments = Comments.new
@rating = Rates.new(:book_id => params[:id], :user_id => session[:user_id])
@previouscomments = Comments.where(:book_id => params[:id] )
end
def post_comment
@comments = Comments.new(params.require(:comments).permit(:content, :book_id, :user_id))
@comments.book_id = params[:id]
if @comments.save
redirect_to(:action => 'showbooks')
else
redirect_to(:action => 'home')
end
end
提交似乎甚至没有发生。我做错了什么?
最佳答案
我认为您指定的 url
无法正常工作。假设 Comments
是您的 Controller 名称:
<%= form_for(:comments, :url => post_comment_comments_url, :method=> "POST" ) do |f| %>
这应该适合你。
关于ruby-on-rails - rails 上的 ruby : form_for doesn't redirect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34812315/