I'm a Rails noob and I'm following a blog post I found here...
I have everything working up until the end. Then things get nebulous.
So if I have this in my helper...
module ApplicationHelper
def display_content_with_links(tweet)
tweet.content.gsub(/(http:\/\/[a-zA-Z0-9\/\.\+\-_:?&=]+)/) {|a| "#{a}"}
end
end
Shouldn't I be able to have my tweets display in my view with this...
What am I doing wrong?
You are going to need a controller and view to have this display. Something simple like:
# app/controller/tweets_controller.rb
TweetsController < ApplicationController
def index
#tweets = Tweet.get_latest
end
end
and in the view:
# app/views/tweets/index.html.haml
%ul
- #tweets.each do |tweet|
%li
= display_content_with_links tweet
or if you use erb
# app/views/tweets/index.html.erb
<ul>
<% #tweets.each do |tweet| %>
<li>
<%= display_content_with_links tweet %>
</li>
<% end %>
</ul>
This is a pretty basic example that might not even come close to what you want, but it should point you in the right direction.
Actually, you should add
#tweet = Tweet.all
To the controller#action you have setup already, then iterate over them in your view:
<ul>
<% #tweets.each do |tweet| %>
<li><%= display_content_with_links tweet %></li>
<% end %>
</ul>
Related
Hello I'm still fairly new to Rails but, currently I have been working on a Rails project for bit now and my last issue with it is when someone clicks on a specific recipe it only shows the very first one a user ever created. I've accessed my database through my console to see if these recipes are saving and they are but when I click on any of the links to a specific recipe it still shows the incorrect one and it won't show the recipe name either.
Here's my recipe controller
class RecipesController < ApplicationController
before_action :require_login
def show
#recipe=Recipe.find_by(params[:name])
binding.pry
end
def index
#recipes =Recipe.all
#binding.pry
end
def new
#recipe = Recipe.new
#recipe.ingredients.build(name: "name")
end
def create
#recipe = Recipe.new(recipe_params)
#recipe.save
#binding.pry
redirect_to recipes_path
end
private
def recipe_params
params.require(:recipe).permit(:id,:name,:content, ingredients_attributes: [
:recipe_id,
:user_id,
:name,
:quantity
]
)
end
end
Index Page
<h1>All Recipes</h1>
<ul>
<% #recipes.each do |recipes| %>
<li><%= link_to recipes.name, recipes %></li>
<% end %>
</ul>
Show Page
<% #recipe.name do |r| %>
<h2> <%= r.name %></h2>
<h2> <%= r.content %></h2>
<%end%>
<h3>Ingredients</h3>
<ul>
<% #recipe.ingredients.each do |ingredient| %>
<li><%= "#{ingredient.name} X #{ingredient.quantity}" %></li>
<% end %>
</ul>
Any help would be appreciated
Thank you!
In your show method it's either one of those
Recipe.find_by(name: params[:name])
# or ...
Recipe.find(params[:id])
...depending on what setup you got going in your routes, the second one is the usual Rails way of doing things.
There are a few issues with your code. In your RecipesController, change the show action code to this:
def show
#recipe = Recipe.find(params[:id])
end
In your index.html.erb view, change the code that iterates through your recipes to this:
<% #recipes.each do |recipe| %>
<li><%= link_to recipes.name, recipe %></li>
<% end %>
And finally, in your show.html.erb view, change the code to this:
<h2><%= #recipe.name %></h2>
<h2><%= #recipe.content %></h>
<h3>Ingredients</h3>
<ul>
<% #recipe.ingredients.each do |ingredient| %>
<li><%= ingredient.name %> X <%= ingredient.quantity %></li>
<% end %>
</ul>
Summary of the changes
In the show action of the RecipesController, you search for the recipe by the id passed in from the view. That id comes from this line:
<%= link_to recipe.name, recipe %>
recipe gets to_param called on it, which returns the id of that particular recipe which you then use in the show action of the RecipesController to find the correct recipe.
In the index.html.erb view, you iterate through all of the recipes, via the #recipes variable, and output each recipe. Since you are outputting each recipe, you normally use recipe instead of recipes as the block variable.
In the show.html.erb view, you don't need to iterate through all recipes because you only have one recipe from the show action of the RecipesController. That recipe is stored in the #recipe variable, so you can use that variable directly in the view.
I'm using the acts_as_votable gem to like and unlike "Deals" in my Ruby on Rails project. My user is set to act_as_voter and my deal is set to acts_as_votable, but for some reason everything is set to like as soon as a new user is created, and they can't unlike the deal. For some reason my list of deals all have an unlike button and it doesn't actually do anything but refresh the page. Here's some of my code.
app/views/catalog/index.html.erb
<ul class="deals_list">
<% #deals.each do |deal| %>
<li>
<div>
...
<div class="favorite">
<% if account_signed_in? and current_account.accountable_type == "Personnel" %>
<%= image_tag("dark-favorite.png") %>
<% if deal.liked_by current_account %>
<%= link_to unlike_deal_path(deal), method: :put do %>
Unlike
<% end %>
<% else %>
<%= link_to like_deal_path(deal), method: :put do %>
Like
<% end %>
<% end %>
<% end %>
</div>
</li>
<% end %>
</ul>
app/controllers/deals_controller.rb
def like
#deal = Deal.find(params[:id])
#deal.liked_by current_account
redirect_back(fallback_location: catalog_index_url)
end
def unlike
#deal = Deal.find(params[:id])
#deal.unliked_by current_account
redirect_back(fallback_location: catalog_index_url)
end
config/routes.rb
resources :deals do
member do
put 'like', to: "deals#like"
put 'unlike', to: "deals#unlike"
end
end
Be sure and read the entire Readme because you're using the library wrong.
To check if a voter has voted on a model, you can use voted_for?. You can check how the voter voted by using voted_as_when_voted_for.
I zeroed in on your problem because I was expecting to see a "?" after the deal.liked_by call, which would indicate a boolean result (by convention, not always the case).
So use this instead:
<% if current_account.voted_for? deal %>
I have a products page that is filterable with pg_search gem. What I would like to do is create a breadcrumb that lists the search params onto the page.
If my search url looks like http://127.0.0.1:3000/products?utf8=%E2%9C%93&query=modern&query=stone&query=limestone&commit=Search I would like to print modern stone limestone to the page.
products_controller.rb
def index
#products = if params[:query]
#albums = Album.where(name: params[:query])
Product.search_for(params[:query])
else
#albums = Album.where(name: 'products')
Product.order(:name)
end
end
index.html.erb
This is what I tried, but realized that it lists ALL the tags, which I only need the tags that are part of the search params
<ul class="product-index-breadcrumb">
<% #products.each do |p| %>
<% p.tags.each do |t| %>
<li><%= t.name %></li>
<% end %>
<% end %>
</ul>
You need to modify your URL e.g array string adding [] if you keep this then it only without [] then it takes only last value like limestone,
after modification URL then it will look like this http://http://127.0.0.1:3000/products?utf8=%E2%9C%93&query[]=modern&query[]=stone&query[]=limestone
then if you write like
<%= params[:query] %>
it will return
["modern", "stone", "limestone"]
now you can run a loop like
<ul>
<% params[:query].each do |query_params| %>
<li><%= query_params %></li>
<% end %>
</ul>
then output is
modern
stone
limestone
I have question and category model. Question model has category_id column.
class Question
belongs_to :category
end
class Category
has_many :questions
end
In my controller I have this:
def index
#categories = Category.all
#questions = Question.all
end
I would like to display all categories and all questions that belongs_to specified category. Also, I would like to display question numbers below each category and made links of them and later it will open new page with clicked question.
This is how I tried to do that:
<% #categories.each do |category| %>
<h1><%= category.name %></h1>
<% #questions.each do |question| %>
<ul>
<li><%= link_to question.id %></li>
</ul>
<% end %>
<% end %>
It should look like this but I get stuck:
Category1
1 2 3 4
Category2
1 2 3 4
Question: How to achieve that I display questions like is show above?
You can do it this way:
Controller:
def index
#categories = Category.all
end
View:
<% #categories.each do |category| %>
<h1><%= category.name %></h1>
<% category.questions.each do |question| %>
<ul>
<li><%= link_to question.id, question_path %></li>
</ul>
<% end %>
<% end %>
Since you said you want to display all of the categories, and the questions that belong to each of those categories, I'm assuming that the index action that you pasted in comes from your categories_controller.
One solution I can think of would be to change the instance variables inside your index. I don't really see a purpose for having the instance variable that references all of your Question objects. This is the one I'm talking about:
#questions = Question.all
Yea, get rid of that. You should be fine with just
#categories = Category.all
Since you want to display all of your categories, that instance variable is necessary. And since you mentioned you want to also display all of the questions that belong to each category, that instance variable is sufficient with the right view. You were on the right track, but instead, just use the #categories instance variable; forget about #questions. Here is what your view should probably look like (you were on the right track above):
<% #categories.each do |category| %>
<h1><%= category.name %></h1>
<% category.questions.each do |question| %>
<ul>
<li><%= link_to question.id, question_path %></li>
</ul>
<% end %>
<% end %>
Also note that in that first line of code, when you start a block, you don't need the <%= , You only need the <%. That's because that first line of the block is purely ruby in itself, it isn't actually getting printed to the resulting html.
Hope I helped a little bit!
* Also: I saw another answer on here which is missing something: When you use the <%= link_to %> helper, you need to specify the first argument which is the resulting markup (In this case you wanted the question.id) , AND ALSO A SECOND ARGUMENT, which is the path for the link to follow *
Well, i have a problem, and i was wondering if it could be solved with rails only.
I have setup a view (home.html.erb) vith 3 partials, like this:
<%provide :title, 'Reader'%>
<div class = "row">
<div class = "span4">
<div class = "row">
<%= render 'layouts/add_subscription'%>
</div>
<div class = "row">
<%= render 'layouts/subscription_list'%>
</div>
</div>
<div class = "span8">
<div class = "row">
<%= render 'layouts/view' %>
</div>
</div>
</div>
where subscription_list shows up a list of links pointing to the list action after a redirection, each of them with the id of the subscription:
<ul>
<% current_user.subscriptions.each do |s| %>
<li><%= link_to s.url, "/list?s_id=#{s.id}" %></li>
<% end %>
</ul>
So, each of these links points to the list action in the controller, which tries to fetch the feed list of the subscription just clicked, and update the home view with the list of titles for the selected subscription:
def list
s_id = params[:s_id]
feed = ""
if !s_id.blank?
s = Subscription.find_by(id: s_id)
feed = Feedzirra::Feed.fetch_and_parse(s.url)
#render partial: "layouts/view", :locals => {:f => feed}
end
The problem is that I'm stuck at this point. I've tried to do a redirect_to home_path with feed as a parameter, and even a render (the line before the end of the list method) to see what happened, but nothing updates 'just' the layouts/view partial:
<ul>
<% if defined? feed and !feed.blank? %>
<% f.entries.each do |entry|%>
<li><%= entry.title %></li>
<% end %>
<% end %>
</ul>
So, I was wondering if it's possible to update the partial and see the result after a page reload using only rails methods, or if it can/must be done using javascript, and a clue to how to do this. Thanks in advance.
The goal you want to achieve is to show feed entries in the home.html.erb after clicking a link.
You can do it by pointing your links to the home action instead of list so that rails will automatically render your home.html.erb view and
you have to assign the instance variable #feed so it will be visible in your view.
You can do it like this (refactored a bit):
controller
def home
s_id = params[:s_id]
if s_id.present?
s = Subscription.find_by(id: s_id)
#feed = Feedzirra::Feed.fetch_and_parse(s.url)
end
end
layout/view
<ul>
<% if #feed.present? %>
<% #feed.entries.each do |entry|%>
<li><%= entry.title %></li>
<% end %>
<% end %>
</ul>
I'm not sure what is the path to your action, I assume here that home is the root ("/")
layouts/subscription_list
<ul>
<% current_user.subscriptions.each do |s| %>
<li><%= link_to s.url, "/?s_id=#{s.id}" %></li>
<% end %>
</ul>