我使用带注释的多态关联。当我尝试添加'编辑'并且还“摧毁”#39;在show template中,我在标题中得到错误(现在只编辑)。如何将两个链接添加到show?
comments_controller.rb
class CommentsController < ApplicationController
....
before_action :signed_in_user, only: [:new, :edit]
before_filter :load_commentable
def index
@commentable = load_commentable
@comments = @commentable.comments
end
def show
end
def edit
@commentable = load_commentable
end
def new
@commentable = load_commentable
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(comment_params)
@comment.user = current_user
if @comment.save
redirect_to @comment, notice: "Created."
else
render :new
end
end
def update
@comment = @commentable.comments.build(comment_params)
@comment.user = current_user
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'It was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
private
def load_commentable
resource, id = request.path.split('/')[1, 2]
@commentable = resource.singularize.classify.constantize.find(id)
end
....
end
show.html.erb模板
<%= link_to "Edit", [:edit, @commentable, :comment] %>
形式
<%= form_for [@commentable, @comment] do |f| %>
....
日志
由CommentsController处理#显示为HTML
参数:{&#34; post_id&#34; =&gt;&#34; 1&#34;,&#34; id&#34; =&gt;&#34; 2&#34;}
注释加载(0.3ms)SELECT&#34;注释&#34;。* FROM&#34;注释&#34;哪里 #&34;注释&#34;&#34; ID&#34; =?限制1 [[&#34; id&#34;,&#34; 2&#34;]]
后载(0.2ms)SELECT&#34;帖子&#34;。* FROM&#34;帖子&#34;在哪里&#34;帖子&#34;。&#34; id&#34; = ?限制1 [[&#34; id&#34;,&#34; 1&#34;]]
用户负载(0.2ms)SELECT&#34;用户&#34;。* FROM&#34;用户&#34;用户&#34;。&#34; id&#34; = ? ORDER BY&#34;用户&#34;。&#34; id&#34; ASC LIMIT 1 [[&#34; id&#34;,2]]
(0.2ms)SELECT COUNT(*)FROM&#34; comments&#34;在哪里&#34;评论&#34;。&#34; user_id&#34; = ? [[&#34; user_id&#34;,2]] CACHE(0.0ms)SELECT COUNT(*)FROM&#34; comments&#34; 在哪里&#34;评论&#34;。&#34; user_id&#34; =? [[&#34; user_id&#34;,2]]
在布局/应用程序(10.7ms)内呈现注释/ show.html.erb
在19毫秒内完成500内部服务器错误ActionView :: Template ::错误(没有路线匹配{:action =&gt;&#34;编辑&#34;, :controller =&gt;&#34;评论&#34;,:post_id =&gt;#,:id =&gt; nil, :format =&gt; nil}缺少必需的键:[:id]):
25: <div class="thumbsdown"><%= link_to image_tag('othericons/thumbiconDown.PNG', height: '20', width: '20'),
&#34;#&#34; %GT;
26:&lt;%= link_to image_tag(&#39; othericons / flagicon.PNG&#39;,身高:&#39; 20&#39;,宽度:&#39; 18&#39;),&#34;# &#34; %GT;
27:
28:&lt;%= link_to&#34;编辑&#34;,[:编辑,@注释,:评论]%&gt; 29:
30:
31:app / views / comments / show.html.erb:28:in `_app_views_comments_show_html_erb ___ 2937579164590753686_69833853514120&#39;Rendered /home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(1.6ms)呈现 /home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)呈现 /home/action/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb 在救援/布局(19.1ms)内
路线:
resources :posts do
resources :comments
end
答案 0 :(得分:2)
更改Edit
链接,如下所示:
<%= link_to "Edit", edit_post_comment_path(@commentable, @comment) %>
您已将Post和Comment设置为嵌套路线,因此您需要将Post的对象以及Comment传递给编辑路径。
修改强>
对于多态关联,您可以按如下方式使用它:
<%= link_to "Edit", [:edit, @commentable, @comment] %>