Create a link from a div - Ruby on Rails - ruby-on-rails

Currently I have a text link within the div that links correctly <%= link_to 'Show', profile %>. I have read that the below is the proper way to turn a div into a link
<%= link_to root_path do %>
<div>Hey!</div>
<% end %>
Though when I apply that to my block it gives me a undefined local variable or method 'profile'
<%= link_to profile do %>
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to 'Show', profile %>
</div>
</div>
<% end %>
</div>
<% end %>
My original block without the attempt at linking:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to 'Show', profile %>
</div>
</div>
<% end %>
</div>
My Controller:
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy, :id]
# GET /profiles
# GET /profiles.json
def index
#profiles = Profile.all
end
# GET /profiles/1
# GET /profiles/1.json
def show
end
# GET /profiles/new
def new
#profile = Profile.new
end
#...

Your first example is problematic in many ways - you have links inside links. I'm not exactly sure what you're looking to output from this - but basically you need the
<%= link_to root_path do %>
<div>Hey!</div>
<% end %>
behavior to replace where you have <%= link_to 'Show', profile %>
Something along the lines of:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
<%= link_to profile do %>
<div>
fancy pants stuff here
</div>
<% end %>
</div>
</div>
<% end %>
</div>
If you can give me some guidance on what you want in the div I can post you some code to achieve that, I'm just not clear on what you are looking for exactly.
Another example could be (if you wanted more of the pieces to fall inside that <div></div>:
<div id="profiles" class="transitions-enabled">
<% #profiles.each do |profile| %>
<div class="box panel panel-default">
<div class="panel-body">
<%= link_to profile do %>
<div>
<%= image_tag profile.image.url(:medium) %>
<%= profile.artist %><br/>
... more fancy pants stuff here ...
</div>
<% end %>
</div>
</div>
<% end %>
</div>

Related

Loop through posts that is promote

How to loop through post that are on promoted. I have a method in my post model that I can access. How would I call it in this instance. For example here are my methods my post model.
class Post < ApplicationRecord
def promoted?
subscriptions.present?
end
def self.promoted_posts
Post.joins(:subscriptions).where(:subscriptions => {:is_active => true})
end
def self.not_promoted_posts
Post.left_outer_joins(:subscriptions).where(:subscriptions => {:post_id => nil})
end
def self.ordered_posts
Post.promoted_posts + Post.not_promoted_posts
end
end
Here is my view. I would like to loop through all the posts that are promoted which is the method "def promoted?" in my model
<% #posts.take(6).each do |post| %>
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-4">
<div class="featured-box">
<figure>
<%= link_to image_tag(post.images.first, class: 'img-fluid'), post %>
</figure>
<div class="feature-content">
<div class="product">
<p><%= post.category.name %> / <%= post.subcategory.name %></p>
</div>
<h4><%= link_to post.title, post %></h4>
<ul class="address">
<li>
Ad#: <%= post.ad_number %>
</li>
<li>
posted <%= time_ago_in_words(post.created_at) %> ago
</li>
<li>
<%= post.state%> , <%= post.city.downcase %>
</li>
</ul>
<div class="listing-bottom">
<h3 class="price float-left">$<%= post.price%></h3>
<%= link_to 'Feature Ad', post, class: 'btn-verified float-right', style: 'color: red;' %>
</div>
</div>
</div>
</div>
<% end %>
If you are desperately in need of using the promoted, you can try
Post.all.find_all(&:promoted?)
which is short notation of
Post.all.find_all do |post|
post.promoted?
end
which is kinda n+1 query and opting it while you can just use
Post.joins :subscriptions
is a fail.

(Ruby on Rails) Trying To Edit A Post Using Partials But Having Problems

I'm making a bloglike application. I want to make it so that blog posts are editable if clicked.
This is my partial that displays all the posts of a user and makes them clickable
<% if #feed_items.any? %>
<% #feed_items.in_groups_of(3, false).each do |feeds| %>
<div class="row">
<% feeds.each do |feed| %>
<div class="col-md-4">
<ol class="posts">
<%= link_to edit_post_path(feed) do %>
<%= render feed %>
<% end %>
</ol>
</div>
<% end %>
</div>
<% end %>
<% end %>
This is the Edit view that it redirects to when a post is clicked:
<% if logged_in? %>
<div class="row">
<%= render 'shared/post_form' %>
</div>
<% end %>
And this is the '_post_form' partial which is the layout for an editable post:
<%= form_for(#post) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new post..." %>
</div>
<%= f.submit "Post", class: "btn btn-primary" %>
<% end %>
When I click a post to edit it it does redirect however it gives me the error: "First argument in form cannot contain nil or be empty"
Any suggestions on how to resolve this?
In your controller's edit method you have to set #post like,
def edit
#post = Post.find(params[:id])
end

How to access an instance variables id in the view

I'm using carrierwave to upload photos into my app, the photos have a 'belongs_to' relationship with each location, and the location has a 'has_many' relationship to the photos. Im my show page where the photos for the locations are uploaded they display as normal through a loop. However, in my index page I'm looping through my locations and want to display a photo for that location. The photos have the correct location_id, I'm just not sure on the syntax in the view to access that locations photo. What I would like to do is display one of the photos that correlates to a specific location, something like: #photo(location.id.last). Any help would be much appreciated, thanks in advance.
View
<div class="row">
<!-- featured listings gallery -->
<% #locations.each do |location| %>
<% if location.featured == "true" %>
<div class="col-md-4 col-xs-6">
<div class="thumbnail">
<div class="overlay">
<%= image_tag #photos, :size => '400x300', :class => 'img-responsive' %>
<div class="effect">
<div class="overlay-header">
<%= location.name %>
</div>
<div class="location-link">
<%= link_to "View Building", location_path(location) %>
</div>
</div>
</div>
<h3 class="display-box-caption">
<%= truncate(location.description, length: 100) %><%= link_to 'More', location_path(location), :class => 'more-info' %>
</h3>
</div>
<div class="col-md-12 property-link-container">
<%= link_to "View Building", location_path(location), :class=>"property-link" %>
</div>
</div>
<% end %>
<% end %>
</div>
controller
class PagesController < ApplicationController
def index
#locations = Location.all
#photos = Photo.all
end
def about
end
def faqs
end
def blog
end
def whats_included
end
end
Accessing photo with the help of association:
location.photos.last.photo.url
HTML
<div class="row">
<!-- featured listings gallery -->
<% #locations.each do |location| %>
<% if location.featured == "true" %>
<div class="col-md-4 col-xs-6">
<div class="thumbnail">
<div class="overlay">
<!-- changing #photos to location.photos.last -->
<% if location.photos.any? and location.photos.photo.present? %>
<%= image_tag location.photos.last.photo.url, :size => '400x300', :class => 'img-responsive' %>
<% end %>
<div class="effect">
<div class="overlay-header">
<%= location.name %>
</div>
<div class="location-link">
<%= link_to "View Building", location_path(location) %>
</div>
</div>
</div>
<h3 class="display-box-caption">
<%= truncate(location.description, length: 100) %><%= link_to 'More', location_path(location), :class => 'more-info' %>
</h3>
</div>
<div class="col-md-12 property-link-container">
<%= link_to "View Building", location_path(location), :class=>"property-link" %>
</div>
</div>
<% end %>
<% end %>
</div>
Controller:
def index
#locations = Location.all
# no need to fetch photos now
# #photos = Photo.all
end
Photo Model
class Photo < ActiveRecord::Base
belongs_to :location
mount_uploader :photo, PhotoUploader
end
I see Sahil's post helped. Add .includes(:photos), to load Queries faster. In the future, will be super useful.
def index
#locations = Location.includes(:photos).all
end

Error: param is missing or the value is empty:retweet

I am new to Rails. I want to pass parameters to DB via controller, but I receive this error param is missing or the value is empty:retweet. I think the problem is the way of passing parameters in view.
Here is the view
<% for #p in #posts %>
<div class="panel panel-default post-panel">
<div class="panel-body row">
<div class="col-sm-1">
<img src="/avatar.png" class="rounded-img" height="50px" width="50px">
</div>
<div class="col-sm-11">
<p class="post-title"><%= User.find(#p.user_id).username %> <span class="post-creation">- <%= #p.created_at.to_formatted_s(:short) %></span></p>
<p class="post-content"><%= #p.content %></p>
</div>
<div class="col-sm-12">
<p class="post-links">
<span class="glyphicon glyphicon-comment g-links" aria-hidden="true"></span>
**<%= form_for Retweet.new do |f| %>
<%= hidden_field_tag current_user.id, (:user_id) %>
<%= hidden_field_tag #p.id, (:post_id) %>
<%= f.submit "Retweet", class:"btn btn-primary"%>**
<% end %>
And here is the Controller
def new
#retweet = Retweet.new
end
def create
#retweet = Retweet.new(post_params)
redirect_to(:back)
end
private def post_params
params.require(:retweet).permit(:user_id, :post_id)
end
end
You should read up some tutorials on basic REST controllers in Rails. Your create action didn't save the retweet.
def create
#retweet = Retweet.new(post_params)
if #retweet.save
redirect_to(:back)
else
render action: 'new'
end
end
Edit: Just noticed your form is all wrong:
<%= form_for Retweet.new do |f| %>
<%= f.hidden_field :user_id, current_user.id %>
<%= f.hidden_field :post_id, #p.id %>
<%= f.submit "Retweet", class:"btn btn-primary"%>**
<% end %>
Mind you that you shouldn't allow settign the user_id like this, it is very easy to change it and thus mess around with your data. Instead you should add this to retweet#create:
#retweet.user = current_user

Rails: How to Render Conditional Based Upon 'pages/home.html.erb'

In the tbody section of application.html.erb I wanted to render for the current_user two sidebars if he is on the home page and just one sidebar for every other page.
<body>
<% if current_user.present? %>
<% if 'pages/home.html.erb' %> # Not Working As a Conditional
<div class="container">
<div class="col-md-3">
<%= render 'layouts/valuations' %>
</div>
<div class="col-md-6">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<div id="modal-holder"></div>
<%= yield %>
</div>
<div class="col-md-3">
<%= render 'layouts/sidebar' %>
</div>
</div>
<% else %>
<div class="container-fluid">
<div class="container">
<div class="col-md-9">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
<div id="modal-holder"></div>
<%= yield %>
</div>
<div class="col-md-3">
<%= render 'layouts/sidebar' %>
</div>
</div>
</div>
<% end %>
<% else %>
<div class="container">
<% flash.each do |name, msg| %>
<%= content_tag(:div, msg, class: "alert alert-info") %>
<% end %>
</div>
<%= yield %>
<% end %>
</body>
Firstly, you're looking for current_page?:
<% if current_page?(controller: "pages", action: "home") %>
...
<% end %>
This is notoriously rickety though (what happens if you change your pages controller?).
What you'll be better doing is what beartech suggested, setting a variable which you can ping in your layout:
#app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
#home = true
end
end
#app/views/layouts/application.html.erb
<if #home %>
...
<% end %>
I would match this with the use of partials:
#app/views/layouts/application.html.erb
<%= render "shared/sidebar" if #home %>
<%= render "shared/flash" %>
This gives you the ability to split your code into more manageable chunks. We do it here:
In your :index action for your home page you can set a variable like:
def index
#home_page = true
...
And then you can just do something like:
<body>
<% if current_user.present? %>
<% if #home_page %>
<div class="container">
<div class="col-md-3">
<%= render 'layouts/valuations' %>
</div>

Resources