0

I am relatively new to programming and to rails so please be indulgent:)

I am building a website for myself which contains a blog. I have two models that are nested and I do not seem to understand how to use REST to perform certain actions on my articles and comments.

When I create a comment if the comment doesn't pass validation I want it to render the page again so that the user can correct his mistakes and resubmit the comment. When I try to render, it gives me a missing template error.

Here is the code:

You can also find this code on github --> https://github.com/MariusLucianPop/mariuslp-

routes.rb

Mariuslp::Application.routes.draw do

  get "categories/new"

  root :to => "static_pages#index"

  match "login" => "visitors#login" # not rest
  match "logout" =>"visitors#logout" # not rest
  match "comment" => "articles#show"

  resources :articles do 
  resources :comments
end

  resources :tags, :taggings, :visitors, :categories, :comments


end

articles_controller.rb

def show
  @article = Article.find(params[:id])
  @comment = @article.comments.new
end

comments_controller.rb

def create
    article_id = params[:comment].delete(:article_id)
    @comment = Comment.new(params[:comment])
    @comment.article_id = article_id
    if @comment.save
      redirect_to article_path(@comment.article_id)
    else
      render article_path(@comment.article_id,@comment) ## This one doesn't work
    end
  end

  def new
    @comment = Comment.new
  end

 def destroy 
    Comment.find(params[:id]).destroy
    redirect_to articles_path()
 end

Views-articles: _comment.html.erb

<div class="comment">
<%= comment.body %><br />
<%= link_to "Delete Comment", article_comment_path(@article), :method => :delete,    :confirm => "Are you sure you want to delete this comment?" %>
</div>

_comment_form.html.erb

<%= form_for @comment do |f|%>

    <%= f.hidden_field :article_id%>

    <%= f.label :body %><br />
    <%= f.text_area :body, :cols => 50, :rows => 6 %><br />

    <%= f.submit%>
<%end%>

show.html.erb

<p><%= link_to "<< Back to Articles", articles_path%></p>

<div class = "article_show">
    <%= label_tag :category_id %>
    <%= @article.category_id%> <br />

    <%= label_tag :title%>: 
    <%= @article.title%> <br />

    <%= label_tag :body%>: 
    <%= @article.body%> <br />

    <%= label_tag :tag_list%>:
    <%= @article.tag_list%><br />
</div>

<br />
<% if session[:username]== "marius"%>
<div class ="admin">
    <%= link_to "Edit", edit_article_path(@article)%>
    <%= link_to "Delete", article_path(@article), :method => :delete, :confirm => "Are you sure you want to delete this article ?"%>
</div>
<%end%>
<br />



<%= render :partial => 'comment', :collection => @article.comments %>

<%= render :partial => 'comment_form'%>
Marius Pop
  • 1,431
  • 2
  • 19
  • 32

1 Answers1

3

Have you tried to use where you point the problem?

render 'articles/show'

You don't need to use article_comment_path because that is a full path, not just the place where you store the view templates. In this case, you only need the view. Of couse you must be sure to get all instance variable which you use in this views.

UPDATE:

@article = Articles.find(article_id)
render 'articles/show'
Matzi
  • 13,770
  • 4
  • 33
  • 50
  • 1
    Exactly, you *render* a view, not a path. Although I guess it should be `render 'articles/show'`, cause that seems to be where the commenting is happening. – Mischa May 15 '12 at 13:45
  • It works if I use render 'comments/new' if I have a view named new under comments. The problem is that I want to render the view that the user is initially on. I want to render the view that has all of the other comments there. – Marius Pop May 15 '12 at 13:48
  • Ok I think I understand what you mean. I am testing it right now. – Marius Pop May 15 '12 at 13:53
  • @Matzi It works if I render 'articles/new' but I get another error undefined method `category_id' for nil:NilClass . I am not sure how to overcome it. – Marius Pop May 15 '12 at 14:04
  • Just as I wrote in the updated answer, you must ensure that every instance variable is loaded what you use in the view. Load them before the render. This probably means you must find the `@article` for the article_id. – Matzi May 15 '12 at 14:07