Note that there are some explanatory texts on larger screens.

plurals
  1. POedit link does not work for editing comments in ruby on rails blog
    primarykey
    data
    text
    <p>I am trying to implement edit comment functionality in blog. I am able to create comments on an article and displaying them. When I click "Edit" link for an particular comment of an article, it takes me edit comment form but it does not contains any content. Like when we edit any comment or question on stack overflow, it takes us to edit page with content. But in my case , it takes me to edit page of comment but it is empty( does not contain content of comment). Following is my code files.</p> <p>comments_controller.rb</p> <pre><code> class CommentsController &lt; ApplicationController before_filter :user_signed_in, except: [:create] def new @comment = Comment.new end def create @article = Article.find(params[:article_id]) @comment = @article.comments.build(params[:comment]) @comment.user_id = current_user.id @comment.save flash[:success] = "Comment created!" redirect_to article_path(@comment.article) end def edit @comment = Comment.find(params[:id]) end def update @comment = Comment.find(params[:id]) @article = @comment.article respond_to do |format| if @comment.update_attributes(params[:comment]) redirect_to @article_path(@article) else render :action =&gt; "edit" end end def destroy @comment = Comment.find(params[:id]) @article = Article.find(params[:article_id]) @comment.destroy redirect_to @article_path(@artilce) end end </code></pre> <p>comments/edit.html.erb</p> <pre><code> &lt;h3&gt;Editing comment&lt;/h3&gt; &lt;%= render :partial =&gt; 'comment_form' %&gt; </code></pre> <p>comments/_comment_form.html.erb</p> <pre><code>&lt;%= form_for ([@article, Comment.new]) do |f| %&gt; &lt;%= f.text_area :content, :style =&gt; "width:727px; height:100px; border: 1px solid #999999;margin-top:80px; background-color:#FFFFFF;margin-left:-33px" %&gt; &lt;div class="actions"&gt; &lt;%= f.submit "Add Comment", :style =&gt; "margin-right:20px; margin-left:560px; background-color:#66C9Ef; color:#FFFFFF; border: 0px solid #82b548; border-radius: 3px 3px 3px 3px; font-size: 1.3rem;" %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>comments/_comment.html.erb ( here i have given link for editing comments for an article)</p> <pre><code> &lt;% if @article.comments.count &gt;= 1 %&gt; &lt;div style="border: px solid #66c9ee;border-radius: 0px 0px 0px 0px;margin: 10px -30px 15px; padding: 10px 15px 25px; background: none repeat scroll 0 0 #F2F2F2; width:700px; font-size: 1.2em;border-bottom: 0px solid #DDDDDD;"&gt; &lt;%= comment.content %&gt; &lt;div id="tabula"&gt; &lt;ul id="tabula"&gt; &lt;li&gt; &lt;div style="color: #0077CC;margin-rigth:200px; font-size: 1.0em;margin-top:4px;background-color:#;"&gt; &lt;%= comment.user.username if comment.user %&gt;&lt;/div&gt;&lt;/li&gt; &lt;li&gt; &lt;div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"&gt; &lt;p&gt; &lt;%= time_ago_in_words(comment.created_at.in_time_zone("Asia/Calcutta")) unless comment.created_at.nil? %&gt; &lt;/p&gt;&lt;/div&gt;&lt;/li&gt; &lt;li&gt; &lt;div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"&gt; &lt;%= link_to "edit", edit_article_comment_path(@article ,comment) %&gt; &lt;/div&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% else %&gt; &lt;div style="color:#0077CC;margin-left:25px;font-size:1.4em;"&gt; be first to comment&lt;/div&gt; &lt;% end %&gt; </code></pre> <p>rake routes result.(not complete)</p> <pre><code> articles GET /articles(.:format) articles#index POST /articles(.:format) articles#create new_article GET /articles/new(.:format) articles#new edit_article GET /articles/:id/edit(.:format) articles#edit article GET /articles/:id(.:format) articles#show PUT /articles/:id(.:format) articles#update DELETE /articles/:id(.:format) articles#destroy dashboard_index GET /dashboard(.:format) dashboard#index POST /dashboard(.:format) dashboard#create new_dashboard GET /dashboard/new(.:format) dashboard#new edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit dashboard GET /dashboard/:id(.:format) dashboard#show PUT /dashboard/:id(.:format) dashboard#update DELETE /dashboard/:id(.:format) dashboard#destroy tags GET /tags(.:format) tags#index POST /tags(.:format) tags#create new_tag GET /tags/new(.:format) tags#new edit_tag GET /tags/:id/edit(.:format) tags#edit tag GET /tags/:id(.:format) tags#show PUT /tags/:id(.:format) tags#update DELETE /tags/:id(.:format) tags#destroy article_comments GET /articles/:article_id/comments(.:format) comments#index POST /articles/:article_id/comments(.:format) comments#create new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit article_comment GET /articles/:article_id/comments/:id(.:format) comments#show PUT /articles/:article_id/comments/:id(.:format) comments#update DELETE /articles/:article_id/comments/:id(.:format) comments#destroy GET /articles(.:format) articles#index POST /articles(.:format) articles#create GET /articles/new(.:format) articles#new GET /articles/:id/edit(.:format) articles#edit GET /articles/:id(.:format) articles#show PUT /articles/:id(.:format) articles#update DELETE /articles/:id(.:format) articles#destroy </code></pre> <p>routes.rb</p> <pre><code> Mau::Application.routes.draw do devise_for :users root :to =&gt; 'articles#index' resources :articles resources :dashboard resources :tags resources :articles do resources :comments end end </code></pre> <p>articles_controller.rb</p> <pre><code>class ArticlesController &lt; ApplicationController before_filter :is_user_admin, only: [:new, :create, :edit, :destroy] before_filter :log_impression, :only=&gt; [:show] def is_user_admin redirect_to(action: :index) unless current_user.try(:is_admin?) return false end def log_impression @article = Article.find(params[:id]) # this assumes you have a current_user method in your authentication system @article.impressions.create(ip_address: request.remote_ip,user_id:current_user.id) end def index @articles = Article.all(:order =&gt; "created_at DESC") @article_titles = Article.first(10) @tags = Tag.all end def show @article = Article.find(params[:id]) @related_articles = Article.joins(:taggings).where('articles.id != ?', @article.id).where(taggings: { tag_id: @article.tag_ids }) @article_popular = Article.order('articles.impressions_count DESC').limit(5) end def new @article = Article.new end def create @article = Article.new(params[:article]) @article.user_id = current_user.id if @article.save flash[:success] = "article created!" redirect_to article_path(@article) else render 'new' end end def destroy @article = Article.find(params[:id]) @article.destroy redirect_to action: 'index' end def edit @article = Article.find(params[:id]) end def update @article = Article.find(params[:id]) if @article.update_attributes(params[:article]) flash.notice = "Article '#{@article.title}' Updated!" redirect_to article_path(@article) else render 'edit' end end end </code></pre> <p>articles/show.html.erb (this is only relevant code of show)</p> <pre><code> &lt;div id="commentform"&gt; &lt;%= render :partial =&gt; 'comments/comment_form' %&gt; &lt;% @article.comments.each do |c| %&gt; &lt;% if !c.nil? %&gt; &lt;div id ="commentdisplay"&gt; &lt;%= render partial: 'comments/comment', :locals =&gt; { :comment =&gt; c } %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload