I started using Ruby on Rails a few days ago, and I have a problem with the each do loops.
Here's my AccueilController's code :
def index
#postsTest = Post.last(1)
#articles = Article.last(1)
#articlesList = Article.last(2)
respond_to do |format|
format.html # index.html.erb
format.json { render json: #users }
end
Here's my application.html.erb code :
<div id="container">
<div id="col1">
<%= render :partial => 'shared/listArticles', :collection => #articlesList %>
<div class="clear"></div>
</div>
<div id="col2">
<%= yield %>
</div>
</div>
</div>
And now my shared/listArticles's code :
<% #articlesList.each do |article| %>
<div id="blocArticle">
<div id="pic">
<%= image_tag (article.photo.url(:thumb)) %>
</div>
<div id="contentArticle">
<div id="titleArticle">
<%= link_to article.title, article %>
<div class="clear"></div>
</div>
<div id="contentArticleDesc">
<%= link_to article.desc, article %>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
<% end %>
And my problem is that if I write #articlesList = Article.last(2) then the last two articles will appear two times; if I write #articlesList = Article.last(3) then the last three articles will appear three times etc...
Of course, I would like that each of them appear just one time.
Does someone have any ideas about the source of the problem ?
You're rendering a partial with a collection, therefore Rails calls the partial for each item in the collection. Remove your loop from the partial view or remove the collection param, don't do both!
Related
I am trying to create Comments on User created Articles in Rails 5.1.
After a Comment is submitted, the redirect should be to the '/articles/:id' but is instead redirecting to '/articles/:id/comments'.
I'm using nested routing in routes.rb:
devise_for :users
root to: "articles#index"
resources :articles do
resources :comments
end
My CommentsController.rb:
class CommentsController < ApplicationController
before_action :set_article
def create
unless current_user
flash[:alert] = "Please sign in or sign up first"
redirect_to new_user_session_path
else
#comment = #article.comments.build(comment_params)
#comment.user = current_user
if #comment.save
flash[:notice] = "Comment has been created"
else
flash.now[:alert] = "Comment not created correctly"
end
redirect_to article_path(#article)
end
end
private
def comment_params
params.require(:comment).permit(:body)
end
def set_article
#article = Article.find(params[:id])
end
end
The form for Comments in articles/show.html.erb:
<!--Start of comments-->
<div class="col-md-12">
<%= form_for [#article, #comment],
:html => {class: "form-horizontal", role: "form"} do
|f| %>
<% if #comment.errors.any? %>
<div class="panel panel-danger col-md-offset-1">
<div class="panel-heading">
<h2 class="panel-title">
<%= pluralize(#comment.error.count, "error") %>
prohibited this comment from being saved:
</h2>
<div class="panel-body">
<ul>
<% #comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
</div>
<% end %>
<div class="form-group">
<div class="control-label col-md-2">
<%= f.label :body, 'New Comment' %>
</div>
<div class="col-md-10">
<%= f.text_area :body, rows: 10, class: "form-control", placeholder: "New Comment" %>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<%= f.submit "Add Comment", class: "btn btn-primary btn-lg pull-right" %>
</div>
</div>
<% end %>
</div>
How do I make this submit button save and redirect back to 'articles/:id'? Thanks in Advance.
The routes generated by in your router will look something like this:
/articles/:article_id/comments/:id
This means, when you need to load your article in the CommentsController, you should do something like this (as suggested by #Marlin):
def set_article
#article = Article.find(params[:article_id])
end
Otherwise, you run the risk of attaching the comment to the incorrect article if there happens to be an ID collision between the IDs in the comments and article table. Or you simply get a ActiveRecord::RecordNotFound error.
But I know, that this doesn't answer your question directly, ut I suspect that the issue is that you're loading the wrong record from the db somewhere because of the above mentioned.
Try updating your code, and write a test, to make sure that you can programmatically reproduce the error :)
I'm developing a blog with middle man, I write the articles in markdown. Below is a sample article 2016-01-27-small-and-large-balcony-ideas.html.md
---
title: Small and large balcony ideas
category: "balcony ideas"
---
# Small and large balcony ideas
It uses the following layout layouts/blog.erb
<body class="<%= page_classes %>">
<%= partial "blog_header" %>
<div class="container">
<div class="row">
<div class="column column-75">
<%= yield %>
</div>
<div class="column column-25">
<%= partial "blog_sidebar" %>
</div>
</div>
</div>
<%= partial "blog_footer" %>
</body>
Now when I go to the article.url the page displays heading and the content straightaway. I want to display the article date above or below the main heading. How can I do that.
I add:
<p><%= current_article.date.strftime('%d %B %Y') %></p>
into my layout just above
<%= yield %>
Is that what you are looking for?
I have 2 tabs with bootstrap for user messages - inbox and outbox, and using kaminari
<div id="allmessages" >
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active">Inbox</li>
<li>outbox</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="inbox">
<%= render #incoming_msgs %>
<%= paginate #incoming_msgs %>
</div>
<div class="tab-pane" id="outbox">
<%= render #outgoing_msgs%>
<%= paginate #outgoing_msgs%>
</div>
</div>
</div>
</div>
Problem is, that when going on page 2 in the inbox and then clicking on the outbox tab I get to page 2 of the outbox..
http://localhost:3000/users/messages?page=2#outbox
is it possible to reverse the order or have a pagination per tab?
Passing the param_name option to the paginate method will allow you to page through each object array independently. Right now they are both using the default param name 'page'.
<div class="tab-pane active" id="inbox">
<%= render #incoming_msgs %>
<%= paginate #incoming_msgs, param_name: :incoming_msgs_page %>
</div>
<div class="tab-pane" id="outbox">
<%= render #outgoing_msgs%>
<%= paginate #outgoing_msgs, param_name: :outgoing_msgs_page %>
</div>
Make sure to reference the new param names in the controller
#incoming_msgs = mailbox.inbox.page(params[:incoming_msgs_page]).per(25)
#outgoing_msgs = mailbox.outbox.page(params[:outgoing_msgs_page]).per(25)
short answer - use ajax pagination
http://railscasts.com/episodes/174-pagination-with-ajax
I'm a beginner and there is something I'm a bit confused with.
I have a website that is similar to Metacritic for TV Shows, my two tables are shows and reviews.
There is my show (as in tv show) controller
def show
#show = Show.find(params[:id])
#reviews = #show.reviews.paginate(page: params[:page])
#ratings = #show.reviews.average("rating")
respond_to do |format|
format.html # show.html.erb
format.json { render json: #show }
end
end
and my show view
<p id="notice"><%= notice %></p>
<div class="row">
<aside class="span4">
<section>
<h1>
<p>
<%= #show.name %> ( <%= #show.year %>
)
</p>
<p>
<b>Synopsys:</b>
<%= #show.synopsys %>
</p>
<p>
<b>Note:</b>
<%= #ratings %>
</p>
<%= image_tag #show.avatar.url %>
</h1>
</section>
<section>
</section>
</aside>
<div class="span8">
<% if #show.reviews.any? %>
<h3>Reviews (<%= #show.reviews.count %>)</h3>
<ol class="reviews">
<% #reviews.each do |review| %>
<%= review.content %> <br> <%= review.source %> | <b><%= review.rating %> </b><br><br>
<br><br>
<% end %>
</ol>
<%= will_paginate #reviews %>
Now, I'm trying to do a similar thing on my home view (controller home). I want to add all the shows to the database and their overall ratings. The issue if that when I copy the content of my show controller to my home controller, review is undefined and I don't understand why.
Anyone can help ?
class HomeController < ApplicationController
def index
#shows = Show.all
#ratings = #show.reviews.average("rating")
respond_to do |format|
format.html # show.html.erb
format.json { render json: #show }
end
def about
end
def contact
end
end
end
I am trying to DRY up some of my HTML in my application currently I have a block of HTML that will get re-used multiple times
<div class="block">
<div class="block_head">
<div class="bheadl"></div>
<div class="bheadr"></div>
<h2>Configuration Needed</h2>
</div>
<div class="block_content">
<div class="message warning">
<p>You have not create an admin user yet</p>
</div>
</div>
<div class="bendl"></div>
<div class="bendr"></div>
</div>
What I would like to do is to create a partial or something along those lines and be able to pass in the content to the block header and content
Does anyone know of a way to do this in rails 3
The way i do it is to have a views/shared folder. Then, i create partials inside and i call them like :
<%= render "shared/flash_error", :error => flash[:error] %>
where shared/flash_error is :
<% if error %>
<%= error %>
<% end %>
If you want to have your partials in the partial folder, use this syntax :
<%= render :partial => "partials/your_partial", :locals => { :error => flash[:error] } %>