Rhomobile search question - rhodes

I try to make rhomobile search in Fixed DB.
But it returns ALL entries in DB instead of the ones with given QID
<%=#questions.id%>
<% #antworts = Antwort.find(:conditions=>{'qid'=>#questions.id})%>
<% #antworts.each do |antwort| %>
<li>
<a href="sdfsdf">
<%= antwort.antwort %>
</a>
</li>
<% end %>
</ul>
Any idea why?

You need to pass :first or :all in the first argument:
Antwort.find(:all, :conditions=>{'qid'=>#questions.id})
Or change the method to find_all:
Antwort.find_all(:conditions=>{'qid'=>#questions.id})

Related

Phoenix framework: Where does resource's default form #action attribute for all resources come from?

The command
mix phx.gen.html Blog Post posts title body:text
generates among other files the template form.html.eex for the add/edit posts form, which looks like this:
<%= form_for #changeset, #action, fn f -> %>
<%= if #changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>
<div class="form-group">
<%= label f, :title, class: "control-label" %>
<%= text_input f, :title, class: "form-control" %>
<%= error_tag f, :title %>
</div>
<div class="form-group">
<%= label f, :body, class: "control-label" %>
<%= textarea f, :body, class: "form-control" %>
<%= error_tag f, :body %>
</div>
<div class="form-group">
<%= submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
The form_for function gets #action argument which is not present in the PostController, so where does it come from? I couldn't find.
In my installation I have a plug MyProject.Locale which redirects all unlocalized requests to localized ones like:
/posts --> /de/posts
And I have the :locale scope in my router
scope "/:locale", MyProject.Web, as: :locale do
pipe_through :browser
get "/", PageController, :index
resources "/posts", PostController
end
The default #action doesn't take the scope's locale chunk into account and sends the POST request to the /posts url, which is redirected by my MyProject.Locale plug to /de/posts as GET request (it uses the function Phoenix.Controller.redirect(to:...)). I want the form to send the POST request to the localized path.
So, can we override this #action argument for all resources somewhere in one place or do we have to provide it in each controller in render function call
render(conn, "new.html", changeset: changeset, action: action)
or the only option is to change the form templates for all resources?

Random posts in Hexo

I wanted to show 5 random posts on my home page using Hexo, but can't seem to get it working?! I changed 'date' to 'random' but didn't work.
The code:
<ul>
<% site.posts.sort('date', -1).limit(5).each(function(post){ %>
<li>
<%= post.title || '(no title)' %>
</li>
<% }) %>
</ul>
shuffle or the alias random will work:
<ul>
<% site.posts.random().limit(5).each(function(post){ %>
<li>
<%= post.title || '(no title)' %>
</li>
<% }) %>
</ul>
How it works:
Hexo uses Warehouse for its database. posts is a Query object. So to modify the posts in the future just find the right database Query method in the warehouse API. Each Query method returns a modified copy of the previous Query so that the methods can be chained. So if you want to modify it again just find another method and chain it. Hope this helps!

SIlverstripe <%loop AllChildren %> with Skeleton

I'm using Skeleton for my 16 columns grid system on Silverstripe.
My code below:
<% loop $AllChildren %>
<div class="three columns"><h5>$Title</h5></div>
<% end_loop %>
produces:
Children1 Children2 Children3 Children4 Children5
Children6 Children7 Children8 Children9 Children10
My question is how do I put a space between the rows so the output will
be like:
Children1 Children2 Children3 Children4 Children5
Children6 Children7 Children8 Children9 Children10
In SS3 you can use $MultipleOf() (api docs) in your templates to output something every nth item, e.g.
<% loop $AllChildren %>
<div class="three columns"><h5>$Title</h5></div>
<% if $MultipleOf(5) %>
<hr class="extra-space">
<% end_if %>
<% end_loop %>

How do I use user_helper method globally throughout the app views?

I finished chapter 8 on Michael Hartl's Rails Tutorial, so I have the app set up to sign users up & sign in and out.
What I am trying to do user a User Helper method inside the application.html.erb (or more specifically inside a _header.html.erb partial).
I want to use the gravatar_for method (Defined in the user_helper.rb) to show the user's gravatar picture and users name instead of having the word "Account" with the drop down.
module UsersHelper
# Returns the Gravatar for the given user.
def gravatar_for(user, options = { size: 150})
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "http://gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
On the show.html.erb page I have the following code
<% provide(:title, #user.name ) %>
<div class="row">
<aside class="span6">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
</div>
And that shows the user's gravatar and name on their profile page. So what I tried to do was add those same methods to the header partial like so;
<% if signed_in? %>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<span id="small">
<%= gravatar_for #user %><%= #user.name %><b class="caret"></b>
</span>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li><%= link_to "Sign out", signout_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li id="cta"><%= link_to "+ Sign up", signup_path %></li>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
And that code shows up completely fine on the show.html.erb page, which I am assuming is because its the show action on the Users controller. But when I try to click to the home page or really any other page outside of the Users View then I get the error
NoMethodError in Pages#about
undefined method `email' for nil:NilClass
So I guess my question is how do I make the gravatar for method and the #user.name available to all the views throughout the application. I feel as if the answer should be fairly simple, but i'm pretty new to rails, so any help would be greatly appreciated!
Put the method in the ApplicationHelper instead of the UserHelper. Here is an example of something similar I did in my own app: https://github.com/NatashaTheRobot/tinysale/blob/master/app/helpers/application_helper.rb

Why isn't Template Toolkit aggregating my counter?

I'm working on a simple Dancer app to log books a person has read, but in my template to show how many books a person has read, I'm stumbling into an error. I'm trying to go through ever row in the table of reading instances and add 1 to a counter if the reader is the same as the listed person.
Here's the code for the template:
<ul class="people">
<% IF people.size %>
<% FOREACH id IN people.keys.nsort %>
<li><h2 style="display: inline;"><% people.$id.name %></h2><br />
Born <% people.$id.birthday %><br />
<% FOREACH reader IN readings.keys.nsort %>
<% count = 0 %>
<% IF readings.$reader.person_id == people.$id.id %>
<% count = count + 1 %>
<% END %>
<% END %>
<% count %>
<% END %>
<% ELSE %>
<li><em>Unbelievable. No people here so far</em>
<% END %>
</ul>
However, when I display it, count is only 1. Does anybody know what I'm doing wrong, or do you need more code?
Thanks.
Looks like you need to pull the count initialization out of the FOREACH reader loop:
<% FOREACH id IN people.keys.nsort %>
<li><h2 style="display: inline;"><% people.$id.name %></h2><br />
Born <% people.$id.birthday %><br />
<% count = 0 %>
<% FOREACH reader IN readings.keys.nsort %>
<% IF readings.$reader.person_id == people.$id.id %>
<% count = count + 1 %>
<% END %>
<% END %>
<% count %>
<% END %>