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'%>
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.