Ruby on rails RubyonRails:表单路由到不同的URL
我有一个保存数据的表单,但它被路由到错误的URL 如果我的表格在 localhost:3000/users/1/styles/1 当我提交表格时,我会被重定向到: localhost:3000/styles/1 然后我得到一个错误: 找不到没有ID的用户 views/comments/_form.html.erbRuby on rails RubyonRails:表单路由到不同的URL,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.2,polymorphic-associations,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.2,Polymorphic Associations,我有一个保存数据的表单,但它被路由到错误的URL 如果我的表格在 localhost:3000/users/1/styles/1 当我提交表格时,我会被重定向到: localhost:3000/styles/1 然后我得到一个错误: 找不到没有ID的用户 views/comments/_form.html.erb <%= form_for [@commentable, @comment] do |f| %> <%= f.text_area :content, rows: 3
<%= form_for [@commentable, @comment] do |f| %>
<%= f.text_area :content, rows: 3 %>
<%= f.submit %>
<% end %>
def show
@user = User.find(params[:user_id])
@style = @user.styles.find(params[:id])
@commentable = @style
@comments = @commentable.comments
@comment = Comment.new
end
before_filter :get_commentable
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(params[:comment])
@comment.user = current_user
if @comment.save
redirect_to @commentable, notice: "Comment created."
else
render :new
end
end
private
def get_commentable
@commentable = params[:commentable].classify.constantize.find(commentable_id)
end
def commentable_id
params[(params[:commentable].singularize + "_id").to_sym]
end
resources :styles do
resources :comments, :defaults => { :commentable => 'style' }
end
评论\u controller.rb
<%= form_for [@commentable, @comment] do |f| %>
<%= f.text_area :content, rows: 3 %>
<%= f.submit %>
<% end %>
def show
@user = User.find(params[:user_id])
@style = @user.styles.find(params[:id])
@commentable = @style
@comments = @commentable.comments
@comment = Comment.new
end
before_filter :get_commentable
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(params[:comment])
@comment.user = current_user
if @comment.save
redirect_to @commentable, notice: "Comment created."
else
render :new
end
end
private
def get_commentable
@commentable = params[:commentable].classify.constantize.find(commentable_id)
end
def commentable_id
params[(params[:commentable].singularize + "_id").to_sym]
end
resources :styles do
resources :comments, :defaults => { :commentable => 'style' }
end
routes.rb
<%= form_for [@commentable, @comment] do |f| %>
<%= f.text_area :content, rows: 3 %>
<%= f.submit %>
<% end %>
def show
@user = User.find(params[:user_id])
@style = @user.styles.find(params[:id])
@commentable = @style
@comments = @commentable.comments
@comment = Comment.new
end
before_filter :get_commentable
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(params[:comment])
@comment.user = current_user
if @comment.save
redirect_to @commentable, notice: "Comment created."
else
render :new
end
end
private
def get_commentable
@commentable = params[:commentable].classify.constantize.find(commentable_id)
end
def commentable_id
params[(params[:commentable].singularize + "_id").to_sym]
end
resources :styles do
resources :comments, :defaults => { :commentable => 'style' }
end
如果需要其他信息,请告诉我。为什么要将我重新路由到其他url?我的评论会保存到我的数据库中
谢谢如果您想在创建评论后返回到
localhost:3000/users/1/styles/1
,您应该更改
if @comment.save
redirect_to @commentable, notice: "Comment created."
else
到
编辑:应该使用拥有样式的用户而不是当前用户当您在浏览器的视图源中查看表单HTML时,是否看到正确的URL?(我想不是,但只是问一下,因为你说它“重定向”到了错误的URL)@RajeshKolappakam是的,
action=“/styles/1/comments”
rails获得正确答案的最佳实践是什么?我试图获得的是当前id,而不是当前用户的登录id。它只会将它重定向到正在评论的人。我尝试了params[:user\u id]
但它不正确,这对我有帮助,但我如何保留我正在评论的当前URL的id?只需使用user.find(params[:user\u id])