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

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;
}

Related

How to set dynamic background image of a div ONLY if an image is present in the database

I am attempting to have dynamic background images for multiple divs through index.html.erb, and have accomplished this through inline styling with the background URL being pulled from my database (using carrierwave). It looks something like this right now:
<div class="style-box col-8 ml-auto mr-auto" style="background: url(<%= place.photos.first.picture.url %>)">
<h1><%= link_to place.name, place_path(place) %></h1>
<i><%= place.address %></i>
<br /><br />
<p><%= place.description %></p>
The issue I'm running into is that, when a new place is created, if an image for the new place isn't present (which it wont be immediately upon creation), the view can't create the full html page with incomplete data (ie. place.photos is nil). Is there a way, using this method or something similar, to make the background image overide the default style-box background ONLY if an image for a new place is present? Typically I'd use an if statement, but can't think of a way to make an if statement update CSS with rails.
If you want to keep everything inline you can do:
<div class="style-box col-8 ml-auto mr-auto"
style="background: url(<%= place.photos.first&.picture.url || "/images/default.jpg" %>)">
<h1><%= link_to place.name, place_path(place) %></h1>
<i><%= place.address %></i>
<br /><br />
<p><%= place.description %></p>
</div>
This is using the or operator (||) which lets the first value through if present, or the second as a default. Also, the & will keep the page from having errors if there are no photos. This is essentially the same as writing:
style="background: url(
<% if place.photos.first&.picture&.url %>
<%= place.photos.first&.picture&.url %>
<% else %>
/images/default.jpg
<% end %>
)">
You could also move this into a helper method to let you clean things up, something like:
# in one of the app/helpers modules
def picture_url_or_default(place, default_url = '/images/default.jpg')
return default_url if place.photos.empty
return default_url if place.photos.first.picture.nil?
return default_url if place.photos.first.picture.url.nil?
place.photos.first.picture.url
end
# in the view
<div class="style-box col-8 ml-auto mr-auto"
style="background: url(<%= picture_url_or_default(place))">
...

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

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

Rails: Why does simple_format add a leading newline?

I'm creating an app that has a form where the user enters data into text areas. I want to display these entries, and simple_format is pretty great since it converts the user's end-of-lines into break statements for HTML -- but it's adding a disgusting newline to the front! This is causing the content of 'entry' to be one line lower than it should be (with a blank 'newline' directly above it), and it's throwing off the formatting everywhere on my webpage.
def display_entry(entry)
if entry.blank?
'n/a'
else
simple_format(entry)
end
I tried this:
def display_entry(entry)
if entry.blank?
'n/a'
else
entry.slice!(0) # delete disgusting newline
simple_format(entry)
end
But to no avail -- this merely sliced off the first character of my content, and the disgusting newline persevered. This makes me think simple_format is arbitrarily adding a newline to text that doesn't begin with newline. Why?? And how do I kill it??
EDIT: code from view:
<h2 align="center"> User </h2>
<div id="left_half">
<div class="ibox">
<h3 align="center"> User Info</h3>
<div class="left">
<%= f.label :name, 'name' %>
</div>
<div class ="right">
<%= f.text_field :name, class: 'small-text-area' %>
</div>
...
...
When I display name, it has the leading newline.
<input class="toggle-box" id="id1" type="checkbox">
<label for="id1">User</label>
<div>
<div class="ibox_show">
<div class="left_show">
<%= 'name' %>
</div>
<div class ="right_show">
<%= display_entry(#user.name) %>
</div>
...
...
Are you sure that it's not wrapping entry in
<p>
tags that you need to style?
use this:
style_format(your_Text, html_options = {style: "display: inline"})
this should do the trick to get rid of an ending newline (referred to https://apidock.com/rails/v5.2.3/ActionView/Helpers/TextHelper/simple_format)
simple_format(f.text_field, {}, wrapper_tag: "div",)
the curly braces ARE IMPORTANT as the 2nd parameter. do not leave them out. Simple_format defaults to "p" tag which ends up giving a trailing line break.

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 3: How do you style will_paginate links?

In coding layout is the link styled like
<span>Previous</span>
<a href="#" class="class1 class2 class3"><span>Next</span>
I tried to search and I found some possible modifications of a shape of these two links. It's:
<div class="pag">
<%= will_paginate #data, :page_links => false, :next_label => '' %>
</div>
<div class="pag">
<%= will_paginate #data, :page_links => false, :previous_label => '' %>
</div>
My question: How can I update the second example to the shape of the first example? My problem is those two span elements...
Thanks
It's can be very easy - just add html markup for your paginate components to your config/locales/en.yml file
for example:
en:
hello: "Hello world"
will_paginate:
previous_label: <span class="glyph glyph-prev"></span>
next_label: <span class="glyph glyph-next"></span>
It's all!
http://thewebfellas.com/blog/2010/8/22/revisited-roll-your-own-pagination-links-with-will_paginate-and-rails-3 You can have your own formatter so you can customize anything you want.
Wiki
en:
will_paginate:
previous_label: "← Previous"
next_label: "Next →"
page_gap: "…"
https://github.com/mislav/will_paginate/wiki/I18n#translating-will_paginate-output