Acts_as_taggable_on link_to - ruby-on-rails

I installed the Acts as taggable on plugin to my 'Post' model, however I'm unable to call up a list of posts tagged with certain tag.
Here's what I have in show.
<%= link_to tag.name, posts_path(:view =>'tag', :tag => tag.name) %><% end %>
Except when I click on it, it shows all posts. I want it to show just the posts tagged with that keyword...what am I doing wrong here?
Thanks for your help

Modify your controller code so that it reads something like:
#posts = Post.tagged_with(params[:tag], :on => 'tags')

Here is what I ended up doing...
index view
<% job.tags.each do |tag| %>
<div class="tag"><%= link_to(tag, jobs_path(tag: tag.name), method: :get) %></div>
<% end %>
index action in the controller
if params[:tag]
#search_term = params[:tag]
#jobs = Job.tagged_with(#search_term)
end

Related

Search on Welcome Controller

I have a Product (imovel) controller where the users can create his own Products (imovels). I am using devise for the authentication and the CRUD (create, Update, Delete) needs login. So for the clients be able to see the products and doesn't need a user, I created the Welcome controller where so it is the root.
In the Index of the Products I use Ransack to do the research in the table and works just fine. (very happy about it).
On the welcome controller I try to do the same thing, but when I submit the search the page gets redirect to the imovels#index.
Controller:
class WelcomeController < ApplicationController
def index
#q = Imovel.ransack(params[:q])
#imovel = #q.result(distinct: true)
end
end
View:
<div class="container text-center">
<%= search_form_for #q do |f| %>
# Search if the name field contains...
<%= f.label :descricao_cont %>
<%= f.search_field :descricao_cont %>
<%= f.submit "Pesquisar", class: "btn btn-primary" %>
<% end %>
</div>
Another thing that can be important in the index (imovels#index) there is a for in a tr and the information is filtered there:
Imovels Index
<tbody>
<% #imovels.each do |imovel| %>
<tr>
<td><%= imovel.id %></td>
<td><%= imovel.descricao %></td>
<% end %>
And in the welcome controller where I need the search, I used Divs:
Welcome Index
<% #imovels.each do |imovel| %>
<div class="card">
<div class="containerImovel">
<h4><b><%= imovel.descricao %></b></h4>
<p><%= imovel.cidade %> - <%= imovel.bairro.nome %> </p>
</div>
</div>
<% end %>
How can I do the search on the divs of welcome controller? Ransack is the better option for it? It is possible to search in the has_many :through association?
Add a path for it in your routes.rb
resources :imovel do
collection do
match 'search' => 'welcome#search', via: [:get, :post], as: :search
end
end
and then add a new controller action to WelcomeController
def search
index
render :index
end
and afterwards modify the search form like so
<%= search_form_for #q, url: search_imovel_path, html: { method: :post } do |f| %>
Be sure to recheck the naming/variables as I'm not completely familiar with your app

Rails 5 - Iterating over part of an array for the view

I currently use EasyAutoComplete for a search form. If you hit 'View All' it redirects to the same page but with params[:name] to show all cards.
I render this with:
<% #cards.in_groups_of(6, false).each do |group| %>
<div class='row'>
<% group.each do |card| %>
<div class='col-sm-2 col-md-2'>
<div class="wrapperImg">
<%= link_to image_tag(card.image_url, class: "img-responsive"), {:controller => "cards", :action => "show", :id => card.id }%>
</div>
</div>
<% end %>
</div>
</div>
<% end %>
However, if you look up a specific set of cards it's going to return a couple hundred (or more) of essentially the same card. I can identify these cards by a parameter(rarity)
I was originally going to try to modify it in the controller, but that is an issue because the 'def index' makes the EasyAutoComplete work
def index
wild_search = "%#{params[:name]}%"
#cards = Card.order(multiverse_id: :desc).limit(30)
# debugger
##cards = #cards.where("name like :name", name: wild_search).page(params[:page]) if params[:name]
#cards = #cards.where("name like :name OR setName like :name", name: wild_search).page(params[:page]) if params[:name]
end
Is there a way for me to do something like
cards = #cards.where('rarity IS NOT ?', 'Land') or something similar in the view, then modify my output from #cards.in_group_of to cards.in_group_of? Or is there a way to use the Controller to do this and use def search instead of def index?
Welcome any input.
Like this?
<% #cards.where.not(rarity: "Land").in_groups_of(6, false).each do |group| %>
http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods/WhereChain.html

link to last post - rails

Im trying to link_to the show action for a post.
In my controller I have:
#post = Post.all
In my view I'm trying to link it to the last post like this:
<%= link_to #post.last do %>
<%= #post.last.title %>
<% end %>
But it does not take me to the show page for that post?
Post.all loads all posts, but does not guarantee an order. You might want to use the id or the created_at value to order your list of posts:
# in your controller
#posts = Post.order(:id)
# in the view:
<%= link_to #posts.last.title, #posts.last %>
Or - if you don't need the other posts in the view - just load the lastest post:
# in the controller:
#latest_post = Post.order(:id).last
# in the view:
<%= link_to #latest_post.title, #latest_post %>
Try with below code,
<%= link_to post_path(#post.last) do %>
<%= #post.last.title %>
<% end %>
If this code not work then please find route with fire rake routes in your terminal and replace post_path with your routes
Hope this will work.

How to use one layout by two different actions in rails 4

I'm building a micropost rails app where users can create posts, I created a route and an action to display posts that belong to the signed-in user, happens that the general index layout for posts is exactly the same as the "myposts" layout so instead of duplicating code I would like to use just one layout with different parameters.
This is what I have until now:
routes.rb
resources :posts
get '/myposts', to: 'posts#my_posts', as: 'myposts'
posts_controller.rb
def index
#posts = Post.all
end
def my_posts
#myposts= Post.where(user_id: current_user.id)
end
index.html.erb
<% #posts.each do |post| %>
<div>
<h1><%= link_to post.title, post %></h1>
<%= link_to image_tag(post.meme_url(:thumb)), post, :target => "_blank" %>
</div>
<% end %>
my_posts.html.erb
<% #myposts.each do |post| %>
<div>
<h1><%= link_to post.title, post %></h1>
<%= link_to image_tag(post.meme_url(:thumb)), post, :target => "_blank" %>
</div>
<% end %>
Thanks in advance!
You can use 'render' on 'my_posts' action - http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
Add before 'end' in my_posts action:
render :index

Ruby on rails tags [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Adding tags to posts in Ruby on Rails
I have a simple yet tricky (for me at least) question... what would be the best way to create tags in my sample application blog?
I am adding tags using act-as-taggable, but can I make them clickable so that when people click on it, all posts with that tag would be shown?
I can't quite get it O___o
Any help is super appreciated!
Here is what i did so far:
in my posts controller
def tagged
#posts = Post.all(:order => 'created_at DESC')
#tags = Post.tag_counts_on(:tags)
#tagged_posts = Post.tagged_with(params[:tags])
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #posts }
end
end
then in my posts/show view
<% unless #post.tags.empty? %>
<div class="category">Category:
<% #post.tags.each do |t| %>
<%= link_to t.name, {:tag => t.name, :action => "tagged", :controller => 'posts'} %>
<% end %>
in my posts/tagged view
<% #tagged_posts.each do |post| %>
<div class="entry">
<h2><%= link_to post.title, post %></h2>
<div class="content"><%= sanitize blog_truncate(post.content, :words => 100),:tags => %w(strong, b, a) %><br /><%= link_to "[read more]", post %>
</div>
</div>
<% end %>
I kind of loosely followed this guide:
http://g-p.si/posts/tagging-with-acts-as-taggable-on
my issue is that the tag is clickable on my posts/show page, I get redirected to my tagged page and the url looks like mysite/tagged?tag=ruby
But my tagged page is blank...
Each link for the tag that the user clicks on should have a href of something like:
/posts?tag=my_tag_name
And then in the posts controller
class PostsController
def index
if params[:tag].present?
#posts = Post.where(tag: params[:tag])
else
#posts = Post.all
end
end
end
Note this code is not tested and I've never used acts as taggable so you should first make sure how to query for tagged posts.
It's almost right, thanks for the hint! you have to use
#posts = Post.tagged_with(params[:tag])
instead of
#posts = Post.where(tag: params[:tag])
and it works like magic! :)

Resources