Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - routing when nesting with rails - beginner
    text
    copied!<p>I am relatively new to programming and to rails so please be indulgent:) </p> <p>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. </p> <p>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.</p> <p>Here is the code:</p> <p>You can also find this code on github --> <a href="https://github.com/MariusLucianPop/mariuslp-" rel="nofollow">https://github.com/MariusLucianPop/mariuslp-</a></p> <p><strong>routes.rb</strong> </p> <pre><code>Mariuslp::Application.routes.draw do get "categories/new" root :to =&gt; "static_pages#index" match "login" =&gt; "visitors#login" # not rest match "logout" =&gt;"visitors#logout" # not rest match "comment" =&gt; "articles#show" resources :articles do resources :comments end resources :tags, :taggings, :visitors, :categories, :comments end </code></pre> <p><strong>articles_controller.rb</strong></p> <pre><code>def show @article = Article.find(params[:id]) @comment = @article.comments.new end </code></pre> <p><strong>comments_controller.rb</strong></p> <pre><code>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 </code></pre> <p>Views-articles: <strong>_comment.html.erb</strong></p> <pre><code>&lt;div class="comment"&gt; &lt;%= comment.body %&gt;&lt;br /&gt; &lt;%= link_to "Delete Comment", article_comment_path(@article), :method =&gt; :delete, :confirm =&gt; "Are you sure you want to delete this comment?" %&gt; &lt;/div&gt; </code></pre> <p><strong>_comment_form.html.erb</strong></p> <pre><code>&lt;%= form_for @comment do |f|%&gt; &lt;%= f.hidden_field :article_id%&gt; &lt;%= f.label :body %&gt;&lt;br /&gt; &lt;%= f.text_area :body, :cols =&gt; 50, :rows =&gt; 6 %&gt;&lt;br /&gt; &lt;%= f.submit%&gt; &lt;%end%&gt; </code></pre> <p><strong>show.html.erb</strong></p> <pre><code>&lt;p&gt;&lt;%= link_to "&lt;&lt; Back to Articles", articles_path%&gt;&lt;/p&gt; &lt;div class = "article_show"&gt; &lt;%= label_tag :category_id %&gt; &lt;%= @article.category_id%&gt; &lt;br /&gt; &lt;%= label_tag :title%&gt;: &lt;%= @article.title%&gt; &lt;br /&gt; &lt;%= label_tag :body%&gt;: &lt;%= @article.body%&gt; &lt;br /&gt; &lt;%= label_tag :tag_list%&gt;: &lt;%= @article.tag_list%&gt;&lt;br /&gt; &lt;/div&gt; &lt;br /&gt; &lt;% if session[:username]== "marius"%&gt; &lt;div class ="admin"&gt; &lt;%= link_to "Edit", edit_article_path(@article)%&gt; &lt;%= link_to "Delete", article_path(@article), :method =&gt; :delete, :confirm =&gt; "Are you sure you want to delete this article ?"%&gt; &lt;/div&gt; &lt;%end%&gt; &lt;br /&gt; &lt;%= render :partial =&gt; 'comment', :collection =&gt; @article.comments %&gt; &lt;%= render :partial =&gt; 'comment_form'%&gt; </code></pre>
 

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