When I use the Kaminari in Rails, the structure repeated in view - html

In my controller:
#xxxs = XXX.page(params[:page] || 1).per(10)
In my view:
<div class="turn_page>
<%= paginate #xxxs, window: 2 %>
</div>
Why the content in the end of the font is repeated again, but inside the tag, only the link has no html content

Related

How to display a models attribute in a read only text area

For background I am using Ruby on Rails with the Devise gem. One of the Users attributes is a string called bio. My goal is for a profile page to display the bio in a read only text box. I feel the lines the text box displays gives a nice aesthetic. I am also using bootstrap.
Pages Controller:
def profile
user=User.all
respond_to do |format|
format.html { render :profile, locals: { user: user } }
end
end
The code for my profile.html.erb
<div class="row">
<div class="col-5">
<%= current_user.text_area :bio, readonly: true %>%
</div>
The error I am getting is NoMethodError in Pages#profile - undefined method `text_area'
Use css in order to achieve the same result as text_area. Place your current_user.bio attribute in a <p> or <div> element and give specific width to that element so the line can break. For example:
<div class="row">
<div class="col-5">
<p class='specific-width'> <%= current_user.bio %> </p>
</div>
</div>
Then in css file:
.specific-width{
width: 30px;
}

How to assign an div id a variable without using Javascript?

I'm trying to create multiple divs inside a for loop on a jsp page. I'm loading all the posts in a forum from a database, each one being a new div. I'm trying to make divs having id like id= "post-" + title, where title is a variable.
I tried with this way of puting out.printlnt(title) and is not working, I also found a solution saying to put smth like div id = {{title}} that still didn't work. Do you know if is possible to do this without using javascript? I just want to assign the id from the for loop.
for (ForumPost fp : allForumPosts) {
//get title and likes variables
<div id = "<%out.println(title);%>" >
<%out.println(title); out.println(likes);%>
<a>LIKE</a>
</div>
}
If you don't mind using jstl (which is preferred way to scriptlets) you can do this the following way:
<c:forEach var="post" items="${allForumPosts}">
<div id="post-${post.title}">
${post.title}; ${post.likes} <a>LIKE</a>
</div>
</c:forEach>
Just make sure to include this tag at the top of your jsp:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
If you want to use scriptlets as you do now, I think you should do something like:
<% for (ForumPost fp : allForumPosts) {%>
<div id="post-<%out.write(fp.title)%>">
<%out.write(fp.title)%>;<%out.write(fp.likes)%>
<a>LIKE</a>
</div>
<% } %>
But you really should consider using jstl instead of scriptlets
Java Server Pages (JSP) is a server-side programming technology for your front-end.
Assuming that you have passed the object from your controller to JSP, then you can achieve the desired for loop with JSTL
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach items="${allForumPosts}" var="post">
<div id="post-${post.title}"></div>
</c:forEach>
If you however prefer scriptlet (not recommended), here is how it's done
<% for (ForumPost post : allForumPosts) { %>
<div id="post-<%=post.title%>"></div>
<% } %>

The "show" page isn't displaying the property of the correct data

I'm making a website using ror and I have a list of fandoms, and I want to display different pictures and writing for that fandom. Right now, just for testing, I'm having it just display the name of the fandom at the top of the page, but no matter which one I click on, it only displays the name of the first fandom. Here's the parts of the files I think are relevant, but if you need to see more, I'll post it.
Section of routes.rb:
get 'fandoms' => 'fandoms#index'
get 'fandoms/new' => 'fandoms#new'
post 'fandoms' => 'fandoms#create'
get 'fandoms/:id' => 'fandoms#show', as: :fandom
resources :fandoms
The show method in fandoms_controller:
def show
#fandom = Fandom.find_by(params[:id])
end
The index.html.erb for fandoms:
<div class="main">
<div class="container">
<h2>Fandoms</h2>
<%= link_to "New Fandom (dev only)", "fandoms/new" %>
<% #fandoms.each do |fandom| %>
<div class="fandoms">
<%= link_to fandom.name, fandom_path(fandom) %>
</div>
<% end %>
</div>
And finally the show.html.erb for fandoms:
<div class="main">
<div class="container">
<h2>Fandoms</h2>
<div class="fandom">
<h1><%= #fandom.name %></h1>
</div>
</div>
</div>
I double checked my code with the Ror tutorial on codecademy, but I still couldn't find any differences for what I wanted to do.
Thanks.
Your show action has a subtle typo that's breaking your logic:
#fandom = Fandom.find_by(params[:id])
Should actually be:
#fandom = Fandom.find(params[:id])
The difference is, find_by will return the first row for which the conditions passed are met. You're not actually passing any conditions; just an ID, which in Postgres doesn't even work:
irb> Fandom.find_by(1)
PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer
LINE 1: SELECT "fandoms".* FROM "fandoms" WHERE (1) LIMIT 1
Your database is more permissive, but seems to be treating it just as a WHERE TRUE clause and thus every single row meets the condition. As such, it returns the first row every time. Using find(id) will give you the desired result.

Rails: Adding an empty tag plus content to link_to

I'm trying to generate a link using the link_to helper that will output the following HTML:
<i class="some_class"></i>Link Name
However the code I'm using to try to accomplish this:
link_to(tag("i", class: options[:icon]) + title, url)
...is outputting:
<i class="some_class">Link Name</i>
Why is it doing this, and how can I fix it? Thanks.
EDIT:
I believe I found the issue.
<i> tags are not self-closable tags in HTML5. Therefore the text after the i is treated as that element's content.
Have you tried using the block format of link_to?
<%= link_to url do %>
<%= tag("i", class: options[:icon]) %>
Link Name
<% end %>
Tweak that to your needs and maybe you'll get what you're looking for.
This is the icon tag helper I use in my applications which I frequently pass as the first argument to link_to, which can either be used to create a icon tag alone, or an icon tag followed by text.
def icon_tag(icon, *args)
options = args.extract_options!
text = args.first || options.delete(:text)
if text.nil?
content_tag :i, "", class: ["icon", "icon-#{icon}"] + options[:class].to_a
else
"#{icon_tag icon} #{text}".html_safe
end
end

Div Tables with ASP.NET MVC

I'm trying to find a better way to use a div table with ASP.NET MVC, the problem I see is that you need to do loads of looping, rather than one loop if I had to use a traditional <table> table.
Example
<div class="column">
<div class="row">Name</div>
<% foreach (Person person in (List<Person>)ViewData.Model) {%>
<div class="row"><%= Html.Encode(person.Name) %></div>
<%} %>
</div>
<div class="column">
<div class="row">Email</div>
<% foreach (Person person in (List<Person>)ViewData.Model) {%>
<div class="row"><%= Html.Encode(person.Email) %></div>
<%} %>
</div>
<div class="column">
<div class="row">Phone</div>
<% foreach (Person person in (List<Person>)ViewData.Model) {%>
<div class="row"><%= Html.Encode(person.Phone) %></div>
<%} %>
</div>
If it looks like a table and smells like a table why not use a table?
But if you want to do it in this way well try to build a extention method for your Html property that generates this html code and have the list as a parameter and maybe a list for your columns, To generate your html code you can use the TagBuilder class.
It's rather subjective issue, but I think you don't really need to use divs to do so, coz you are simply displaying table of data and this is why we need "table" tag.
The introduction of div and CSS layout is not to replace the table tag, but to free table tag from doing formatting and layout job.
Edit
Moreover, you can still do your job in one loop. Rather than loop through columns, why not loop through rows (name, phone... )
<% foreach(Person person in (List<Person>)ViewData.Model)) %>
<div class="row">
Name: <%= Html.Encode(person.Name) %>
Email: <%= Html.Encode(person.Email) %>
...
</div>
Although I personally still prefer using table (together with tr and td) instead.
If you are dislaying tabular data, that's why the table tag is there. People only suggest div, instead of table when it's about layout. So it's perfectly OK to use the table tag when you are displaying key-value type of information.
I agree with what everyone else have said (that you really should just use a table) - however I will try to come up with a solution to your issue too.
I don't think there's an elegant way to overcome the "loop more than once" issue, but at the very least we can make it a bit "easier" to add new columns to the list:
var myList = (List<Person>)ViewData.Model;
var myColumns = new Dictionary<string, List<string>>();
myColumns.Add("Name", new List<string>());
myColumns.Add("Email", new List<string>());
myColumns.Add("Phone", new List<string>());
foreach(var person in myList){
myColumns["Name"].Add(Html.Encode(person.Name));
myColumns["Email"].Add(Html.Encode(person.Email));
myColumns["Phone"].Add(Html.Encode(person.Phone));
}
Then now you can do this:
<% foreach(var column in myColumns){ %>
<div class="column">
<div class="row"><%= column.Key %></div>
<% foreach (string value in column.Value) {%>
<div class="row"><%= value %></div>
<%} %>
</div>
<% } %>
It is still poor performance compared to using the -tag and really I don't see why you'd want to avoid that in this scenario.