I have a Job model where I save a bunch of attributes, one of which is a link attribute as a string which contains a url to an external site. I want to put this link on my view but when I try to render it, it does not work.
Is there some reason that rails blocks urls from being shown? All my other attributes show up fine and when I try on rails console I can see the .link attribute showing up as a string.
Here is my link_to tag
<%= link_to "Visit Website", job.link , class: "detail" %>
The problem is that job.link does not even evaluate on the view. But it works on rails console.
My full code is below
Model:
class Job < ActiveRecord::Base
def self.search(search)
where("position LIKE ?", "%#{search}%")
end
end
Controller:
class JobsController < ApplicationController
def index
#jobs = Job.paginate(:page => params[:page], :per_page => 10).search(params[:search])
end
end
Job partial
<% if #jobs.present? %>
<div id="integration-list">
<ul>
<% #jobs.each do |job| %>
<li>
<div>
<a class="expand">
<div>
<h2 class="position"><%= job.position %> </h2>
<span class="info"> <%= job.company %> <br> Due Date: <%= job.date %></span>
</div>
</a>
<%= link_to "Visit Website", job.link , class: "detail" %>
</div>
</li>
<% end %>
</ul>
<%= will_paginate #jobs, :class => 'jobsite_pagination' %>
</div>
There are no posts containing the term(s): .
Try below code, use job.link instead of #{job.link}
<%= link_to "Visit Website", job.link , class: "detail" %>
Related
I have a search form that searches through posts but I want users to be able to filter these searches based on various attributes. How do I link the checkbox filters to the search? I am using sunspot gem to filter through the posts.
This is post.rb
class Post < ActiveRecord::Base
belongs_to :user
searchable do
text :details
text :title
end
end
This is show.html.erb in posts folder
<div class = "container">
<% #posts.each do |x| %>
<div class="fb" id="download">
<div class="post">
<div class="top">
<div class="name">
<strong><span class="text-name"><%= x.title %></span></strong>
</div>
</div>
<span class="text-message"><%= x.details %></span><br><br>
</div>
</div>
<% end %>
</div>
This is show function in my posts controller
def show
#search = Post.search do
fulltext params[:search]
end
#posts = #search.results
end
And this is the search form in my application.html.erb
<% if current_user.profile %>
<li><%= form_tag user_post_path(user_id: current_user.id), :method => :get do %>
<p>
<%= text_field_tag :search, params[:search], autocomplete: "off", placeholder: "Try \"Tennis\"" %>
<button type="submit"><i class="fa fa-search"></i>
<% end %></li>
<li><%= link_to "Create Post", new_user_post_path(user_id: current_user.id), class: "linkfocus"%></li>
<li><%= link_to "Profile", user_path(id:current_user.id), class: "linkfocus"%></li>
<% else %>
I have an application set up with articles, a home page, and a search function. I am using elasticsearch for my search functionality. I haven't been able to come up with a way to use the search form on my home page to redirect to the query results. I feel like this should be simple, but I'm just missing it.
Note: Elasticsearch works, just within it's own view folder. I simply want the search form on my home page to operate the same way and take me to a "results" page that shows a list of queried articles.
My search controller:
class SearchController < ApplicationController
def search
if params[:q].nil?
#articles = []
else
#articles = Article.search params[:q]
end
end
end
My search view:
<h1>Articles Search</h1>
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :q, params[:q] %>
</p>
<% end %>
<ul>
<% #articles.each do |article| %>
<li>
<h3>
<%= link_to article.title, controller: "articles", action: "show", id: article._id%>
</h3>
</li>
<% end %>
</ul>
The search form on my home page:
<div class="search">
<%= form_for search_path, method: :get do |f| %>
<%= text_field_tag :q, params[:q], placeholder: "Search..."%>
<%= submit_tag "Go", name: nil %>
<% end %>
</div>
My search route:
get 'search', to: 'search#search'
I have a page that displays a users videos that he has posted and also on the same page a users videos that he has voted on.
here is my controller
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#videos = #user.videos.order("created_at desc").page(params[:page]).per_page(12)
votes = #user.videos_with_votes("created_at desc").page(params[:page]).per_page(12)
end
end
Here is my view for my show page
<div id="videos">
<% #videos.each do |video| %>
<div class="box">
<%= image_tag video.image.url(:threeacross) %>
<%= video.title %>
<%= video.description %>
<%= video.user_id %>
<%= link_to 'Show', video %>
<%= link_to 'Edit', edit_video_path(video) %>
</div>
<% end %>
</div>
I am confused on how to display the videos that the user has voted on in the same way that i am looping the users video posted?
Use an instance variable, e.g.
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#videos = #user.videos.order("created_at desc").page(params[:page]).per_page(12)
#voted_videos = #user.videos_with_votes("created_at desc").page(params[:page]).per_page(12)
end
end
<div id="voted_videos">
<% #voted_videos.each do |video| %>
<div class="box">
<%= image_tag video.image.url(:threeacross) %>
<%= video.title %>
<%= video.description %>
<%= video.user_id %>
<%= link_to 'Show', video %>
<%= link_to 'Edit', edit_video_path(video) %>
</div>
<% end %>
</div>
If the display of videos and voted videos is the same than you can look to make this code:
<div class="box">
<%= image_tag video.image.url(:threeacross) %>
<%= video.title %>
<%= video.description %>
<%= video.user_id %>
<%= link_to 'Show', video %>
<%= link_to 'Edit', edit_video_path(video) %>
</div>
a partial called _videos.html.erb that can be used for both #videos and #voted_videos.
Store this file (with the above code) in the same directory as the other view files
e.g.
<div id="voted_videos">
<% #videos.each do |video| %>
<% render "videos" %>
<% end %>
</div>
<div id="voted_videos">
<% #voted_videos.each do |video| %>
<% render "videos" %>
<% end %>
</div>
You need to use #votes instead of votes by itself. The # symbol shares the variable with the view layer so that you can do things with it (like loop over it).
there is a concept about instance variable and local variable in rails.
whenever you define a variable with # symbol that particular variable is an instance variable and is accessible across its views and partials.
for eg : #videos = #user.videos.order("created_at desc").page(params[:page]).per_page(12)
here #videos is an instance variable and persists its instance across its scope.
whereas local variables have its scope limited to its loop/boundry. Local variables are preferably used in loops where after the execution of the loop the variable is Garbage Collected and we can again use the same variable name for storing different value.
In your case votes is not accessible in your view as its a local variable, and hence to use this variable in your view you need to define this variable as instance variable (as #votes).
I am using the Public_Activity gem to display the list of actions being performed on the website. I already have a main feed on the homepage but I would also like to include all of the activities that are being tracked using that gem.
Here's what I am trying to do inside the controller
class StaticPagesController < ApplicationController
def home
if signed_in?
#micropost = current_user.microposts.build
#feed_activities = PublicActivity::Activity.order("created_at desc")
#feed_posts = current_user.feed.without_review.paginate(page: params[:page])
#feed_items = #feed_posts + #feed_activities
end
end
Then in my view, this is how I'm trying to call it
<% if #feed_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/feed_item', collection: #feed_items %>
</ol>
<%= will_paginate #feed_items %>
<% end %>
the _feed_item.html being called above is
<li id="<%= feed_item.id %>">
<br>
<% if #feed_activities? %>
<%= link_to activity.owner.name, activity.owner if activity.owner %> has posted
<% else %>
<div class ="gravatarhome"><%= link_to gravatar_for(feed_item.user), feed_item.user %></div>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="textname">shared this</span><span class="timestamp"> <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<div class="FeedContent"><%= truncate(feed_item.content, :length=>150, :omission=>' ...(continued)') %>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
confirm: "You sure?",
title: feed_item.content %>
<% end %><br>
<% end %>
</li>
However, I'm getting this error
NameError in Static_pages#home
Showing C:/app/views/shared/_feed_item.html.erb where line #5 raised:
undefined local variable or method `activity' for #<#<Class:0x6ef3b98>:0x60d50d8>
anyone know if there's a better way to do this and where the activity variable needs to be defined to get this to work?
Update: here's my application_controller that uses the gem
class ApplicationController < ActionController::Base
include PublicActivity::StoreController
The error have stated the problem, rails does not know which activity refer to.
Neither you have activity loaded in the controller, or it is associated to feed_item
Or not rails does not know which activity refer to
activity is a variable that is not defined in this partial, so it will give error.
If you want to loop all activities via collection, then you have to pass variable as collection while rendering the partial and calling using partial name like this:
<%= link_to feed_item.owner.name, feed_item.owner if feed_item.owner %> has posted
Replace activity by feed_item in your partial. It will solve your problem.
I would like to capture email addresses with this form:
I have decided to create a model only for this and use the views and controller for a different model that is serving only static assets (think: newsletter sign-up).
<%= form_for (#signup) do |f| %>
<% if #signup.errors.any? %>
<div id="error_explanation">
<p><%= pluralize(#signup.errors.count, "error") %> prohibited this post from being saved:</p>
<ul>
<% #signup.errors.full_messages.each do |user| %>
<li><%= user %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email_address %><br />
<%= f.email_field_tag :email_address %>
</div>
<div class="actions">
<%= f.submit %>
</div>
How do I get this form to work? I get undefined method signups_path for #<#<Class:0x00000004f1feb0>:0x00000004f09a98> exception, which is understandable (its looking for a SignupsController by convention).
But I want the form to display in a separate controller I call PagesController and send it to the Signup model.
Additional Info:
I tried passing url: pages_path in the form and get the same exception.
# view inside PagesController
<div class="four columns">
<%= render 'form' %>
</div>
#stub of model
class PagesController < ApplicationController
def index
#signup = Signup.new
end
def create
#signup = Signup.new(params[:signup])
end
end
try:
<%= form_for #sighup, :url => {:controller => :pages, :action => :index} do |f| %>
and I hope you didn't forget the
<% end %>