Stuck completing my first rails app - ruby-on-rails

I was taking a tutorial online on rails but I got stuck. These are my controller and view files:
/app/controllers/todos_controller.rb:
class TodosController < ApplicationController
def index
#todo_array = [ "Buy Milk", "Buy Soap", "Pay bill", "Draw Money" ]
end
end
/app/views/todos/index.html.erb:
<title>Shared Todo App </title>
<h1>Shared Todo App</h1>
<p>All your todos here</p>
<ul>
<% #todo_array.each do |t| %>
<li> #todo item here </li>
<% end %>
</ul>
I need to change the code #todo item here to show the actual todo item from the array. So I get output as:
Shared Todo App
All your todos here
- Buy Milk
- Buy Soap
- Pay bill
- Draw Money

<title>Shared Todo App </title>
<h1>Shared Todo App</h1>
<p>All your todos here</p>
<ul>
<% #todo_array.each do |t| %>
<li> <% t %> </li>
<% end %>
</ul>
But since you're getting stuck on that part, I suggest you start with a different tutorial or book, something that explains a bit more about what you're doing and why. http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

Instance variables in your controller are passed as instance variables in your view.
Controller:
#todo_array = [ "Buy Milk", "Buy Soap", "Pay bill", "Draw Money" ]
View:
<% #todo_array.each do |t| %>
<li> <%= t %> </li>
<% end %>

Just replace the comment by <%= t %>. Rails will automatically display each value of the array :
<% #todo_array.each do |t| %>
<li><%= t %></li>
<% end %>

As per the tutorial and the little ruby demo it was mentioned that
Accessing each element of array using blocks. arr.each { |a| puts a } prints all the elements of the array.
However you need to just pass <%= t %> in place of comment.
So your final piece of code will be:
<% #todo_array.each do |t| %>
<li> <%= t %> </li>
<% end %>

Related

Index View Error wrong amount of arguments

I'm getting an argument error and I am not to sure why. I am still fairly new to using Rails and programming overall. I am currently working on my index page and am just trying to get the description displayed. Below is the view and controller. I am not sure why t.description is being expected to have more than one argument. Is this because of strong params?
View:
<h1> All Tea Blends </h1>
<% #teas.each do |t| %>
<h2><%= link_to t.flavor, tea_path(t.id)%> - <%= t.brand.name %></h2>
<% link_to "Write a review", new_tea_review_path%>
<% end %>
<div>
<p><%= t.description %></p>
</div>
Controller:
class TeasController < ApplicationController
def index
#teas = Tea.order_by_rating.includes(:brand)
end
end
t.description is outside of the loop. Not sure why you'd get wrong number of arguments (t is also used for translations, though). Just move it inside the loop...
<% #teas.each do |t| %>
<h2><%= link_to t.flavor, tea_path(t.id) %> - <%= t.brand.name %></h2>
<% link_to "Write a review", new_tea_review_path %>
<div>
<p><%= t.description %></p>
</div>
<% end %>
I'd also recommed changing your loop variable to tea instead of t. Its more descriptive and avoids conflicts.

How to use Asset API to access liquid file through shopify_api gem ruby on rails

I am new to the shopify app development and I ran into the problem while accessing the template file for example : layout/theme.liquid using the shopify_api gem.
I actually am able to setup the gem properly in the app and deploy it correctly. I can access the products and webhooks and also the whole asset list. see my code :
Controller code
class HomeController < ShopifyApp::AuthenticatedController
def index
# below three access for product webhook and full asset are working fine
#products = ShopifyAPI::Product.find(:all, params: { limit: 10 })
#webhooks = ShopifyAPI::Webhook.find(:all)
#assets = ShopifyAPI::Asset.find(:all, params: {"theme_id": 194896516})
# not working below this line
#templates = ShopifyAPI::Asset.all(:params => {:theme_id=> 194896516}).find{|asset| asset.attributes[:key] == "layout/theme.liquid"}
end
end
Template that renders the value
<% content_for :javascript do %>
<script type="text/javascript">
ShopifyApp.ready(function(){
ShopifyApp.Bar.initialize({ title: "Home" });
});
</script>
<% end %>
<h2>Products</h2>
<ul>
<% #products.each do |product| %>
<li><%= link_to product.title, "https://#{#shop_session.url}/admin/products/#{product.id}", target: "_top" %></li>
<% end %>
</ul>
<hr>
<h2>Webhooks</h2>
<% if #webhooks.present? %>
<ul>
<% #webhooks.each do |webhook| %>
<li><%= webhook.topic %> : <%= webhook.address %></li>
<% end %>
</ul>
<% else %>
<p>This app has not created any webhooks for this Shop. Add webhooks to your ShopifyApp initializer if you need webhooks</p>
<% end %>
<hr>
<h2>Assets</h2>
<% if #assets.present? %>
<% #assets.each do |asset| %>
<li><%= asset.key %></li>
<!-- <li><% asset.value %></li> -->
<% end %>
<% else %>
<p>This app has not created any webhooks for this Shop. Add webhooks to your ShopifyApp initializer if you need webhooks</p>
<% end %>
<h2>Template</h2>
<% if #templates.present? %>
<% #templates.each do |template| %>
<li><%= template.key %></li>
<li><% template.value %></li>
<% end %>
<% else %>
<p>This app has not created any webhooks for this Shop. Add webhooks to your ShopifyApp initializer if you need webhooks</p>
<% end %>
I have also tried below variations on my controller but no luck
#templates = ShopifyAPI::Asset.find(:all, params: {"theme_id": 194896516, "asset[key]": 'layout/theme.liquid'})
Also
#templates = ShopifyAPI::Asset.find('layout/theme.liquid',params: {"theme_id": 194896516})
I have set the scope properly for read_themes. I checked the documentation and tried that too but couldn't succeed. Is there anything that I am missing or has the pattern changed over these years as the examples on documentation is not working.
The error I saw on the app is https://prnt.sc/jgglbf .
I hope anyone can help me with this. Really appreciate looking into this question.
Thanks

Getting extra information when looping in Ruby

I'm using the each do loop correctly, and not getting errors when looping an active record base. But for some reason, I am getting extra information at the end.
Here's what my controller looks like:
def archivedBlogs
#compsci = Compsci.all
#personalb = Personalb.all
end
And here is the code I have in the view page:
<div class="panel">
<ul>
<%= #compsci.each do |blog| %>
<li><%= blog.title %></li>
<% end %>
</ul>
</div>
But as you can see, I'm getting extra stuff at the end:
How can I fix it so that it only prints the blog titles?
Change
<%= #compsci.each do |blog| %>
to
<% #compsci.each do |blog| %>

Need to group shows by day in a rails Application

I am trying to get shows on a certain day to show up just for that day and I would like to be able to see what shows are going on in the next couple of days as well.
The View:
<% t = Time.new %>
<h2 class="center" style="color:#2A2C2B"><u><%= t.strftime("%A, %B %e") %></u></h2>
<% #clubs.each do |club| %>
<!-- # <% club.shows.future.present? %> -->
<h1 class="club"><%= link_to club.name, club.website %> </h1>
<% club.shows.future.each do |show| %>
<h3 class="center"><%= show.pretty_start_time %></h3>
<% show.comedians.each do |comedian| %>
<div>
<ol>
<h4 class="comedian"><%= link_to simple_format(comedian.name),comedian_path(comedian) %></h4>
<p class="bio"><u><%= comedian.bio %></u></p>
</ol>
</div>
<% end %>
<% end %>
<%- end -%>
The Comedy Hub Controller:
class ComedyHubsController < ApplicationController
def show
#clubs = ComedyClub.all
end
end
This might work:
<% shows.goup_by{|show| show.date}.each do |dategroup| %>
<# do something to indicated the group up here? %>
<%= dategroup.each do |show| %>
<li> <%# put details here %> </li>
<% end %>
<% end %>
Assuming, there is a DateTime column on Show called start and the ComedyClub model has a a has_many :shows and the Show model has the belongs_to :comedy_club association defined
In your ComedayHubsController#show method, do the following query
#clubs = ComedyClub.join(:shows).where("shows.start >=?", DateTime.now).order("shows.start ASC")
Now only clubs with shows in the future will be sent to the view and already ordered by start.

update partial with object content in rails 4

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>

Resources