rails3 helper method return size - html

I have a gravatar method in the User_helper of a Rails3 application. The code is below:
module UsersHelper
def gravatar_for(user, options = {:size => 50})
gravatar_image_tag(user.email.downcase, :alt => user.name,
:class => "gravatar",
:gravatar => options)
end
end
Currently this method sets the default size as 50px yet this is changed in some implementations throughout the app. I want the gravatar to become part of the "round" class (as well as the "gravatar" class) when the size of the gravatar is greater than 30px. How would I do this?
Thanks in advance :)

module UsersHelper
def gravatar_for(user, options = {:size => 50})
options[:size] > 30 ? #class = "gravatar round" : #class = "gravatar"
gravatar_image_tag(user.email.downcase, :alt => user.name,
:class => #class,
:gravatar => options)
end
end

Related

Display table data from RethinkDB in Phoenix Framework

I'm attempting to display data from my databases in RethinkDB (using the rethinkdb-elixir package from Hamiltop https://github.com/hamiltop/rethinkdb-elixir) in Phoenix. I'm relatively new to both, but I already managed to insert two tables and some data into those tables. I know this because I checked it through RethinkDB's web GUI.
Now I want to display table data in an html page of my project.
I've reduced the errors to one:
protocol Phoenix.HTML.Safe not implemented for %RethinkDB.Collection{data: [%{"first_name" => "Carlos", "id" => "4be8adc3-0973-45dc-bdb8-7a4dac6528d5", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "c84658fc-e4a4-4cb6-8107-b011ca996abd", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "c09fe081-379a-4334-97a3-31c5503c8c61", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "cf0c0ad3-3152-40f0-b613-5b051a314b51", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "ca28a714-ed54-4ebd-8707-d53170ead0f7", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "1ea77c0f-538c-4663-be92-499f16996594", "last_name" => "Santos"}, %{"first_name" => "Carlos", "id" => "1ea74846-0860-4ae5-95f5-674860cf7fc6", "last_name" => "Santos"}]}
Clearly it is fetching all the inserted Carlos Santos persons from the table (which I also must prevent but that is not my main issue) but having an error retrieving them to my Phoenix project.
I've got an index page in whose controller I create the tables and data.
Then I added a new page:
router.ex:
get "/users", UsersController, :users
/views/users_view.ex:
defmodule RethinkExample.UsersView do
use RethinkExample.Web, :view
end
users.html.eex:
<div class="jumbotron">
<p><%= #users %>!</p>
</div>
users_controller.ex
defmodule RethinkExample.UsersController do
use RethinkExample.Web, :controller
use RethinkDB.Query
def users(conn, _params) do
q = table("users")
|> filter(%{last_name: "Santos"})
|> RethinkExample.Database.run
|> IO.inspect
render conn, "users.html", users: q
end
end
I deduce that the html code is also incorrect, because this is how I would display the route specific id inside the html tags.
How can I fetch the data successfully and then display it in a html tag?
The problem here is that your data structure in #users is of type %RethinkDB.Collection{} (source) which cannot be output using <%=...%>
You will likely want to iterate over your users to output them. Something like:
<%= for user <- #users.data do %>
<p><%= "#{user["first_name"]} #{user["last_name"]}" %>!</p>
<% end %>
Here we are using a list comprehension to iterate over all the items on the #users.data array. This is a common way to output an array of elements (such as users, blog posts, comments, etc.) in EEx.
You might also want to consider passing q.data though as #users instead of q to prevent having to do #users.data.
As an aside, you can also use pattern matching inside the list comprehension:
<%= for %{"first_name" => first_name, "last_name" => last_name} <- #users.data do %>
<p><%= "#{first_name} #{last_name}" %>!</p>
<% end %>
This is useful if you don't plan on using many of the fields in the map.

Ruby on rails check_box always rendering checked

my rails checkbox is always checked in the dom. How can I make it so when the page loads it's checked if the object is true or not?
= f.check_box :split_funding, {class: "split_funding", :disabled => #survey.completed?, id: "split_funding_#{school.id}", "data-attribute" => "split_funding", "data-id" => school.id}
Pass it the checked attribute like so:
= f.check_box :split_funding, {checked: #survey.split_funding, class: "split_funding", :disabled => #survey.completed?, id: "split_funding_#{school.id}", "data-attribute" => "split_funding", "data-id" => school.id}
I'm not sure what you mean by 'if the object is true', so I am assuming that :split_funding will return true/false. If it's a relation, you can chain :present? on the end to check for presence of the relation.

Rails add required=>true in select_tag of form

I want to show a "This field is required" if a person has not selected any option in options field.I am trying to make :required => true work.
Please find below the code line. Also if there is no direct way it would be great if someone can point out to any java script which can be used in onChange callback to make things work.
<%= select_tag('params[type]',
options_for_select({"abc" => "ABC","xyz" => "XYZ}, nil),
:required => true ,
:class => "fk-select-box",
:multiple => true,
:tabindex => "4",
:prompt => "Select Type") %>
You can pass javascript function like this on change event of select box,
function Change_selectbox()
{
var selectboxid = document.getElementsByClassName("Selectboxid")[0];
var selectboxchange = selectboxid.options[selectboxid.selectedIndex].value;
if (selectboxchange === "nil")
{
document.getElementById("errors_span_id").innerHTML=("<center>This field is required");
document.getElementsByClassName("selectboxid")[0].focus();
return false;
}
}
Other best option you should use model validation.

Rails + AngularJS + Not able to access all the columns of the Single Model at View

In my rails application, I am trying to fetch Logged-In-User details using AngularJS service. But while accessing the JSON from server I am getting only few columns rather than all the column present in model.
Only FirstName, LastName, and Primary Email is visible on page and rest of the column are empty.
I tried by direct hit of URL : http://www.example.com/LoggedInUserInfo.json
Output : {"first_name":"User","last_name":"1","primary_email":"user_one#mailinator.com"}
User Model
class User < ActiveRecord::Base
attr_accessible :blood_group,:city,:country,:first_name,:gender,:mobile_number,
:phone_number,:primary_email,:secondary_email,:state,:street_address,
:street_address2, :username, :zip_code
Controller Method
def logged_in_user_info
#user = User.find(1)
render :json => #user
#render :json => #user.as_json(:only => [:first_name, :last_name,
# :username, :date_of_birth])
end
AngularJS Service
var user_app = angular.module("UserApp", []);
user_app.controller("LoggedInUserInfoCtrl", function($scope, $http) {
$http.get('/LoggedInUserInfo.json').
success(function(data, status, headers, config) {
$scope.logged_in_user = data;
}).
error(function(data, status, headers, config) {
// log error
});
});
Routes
match '/LoggedInUserInfo' => 'dashboard#logged_in_user_info', :via => [:get]
Please suggest some thing to bring all the columns. I think every thing has to be done with Rails Controller. Thanks in advance.
After a lot of googling and lot of changes in controller, got the way to render or fetch specific column at view.
Just created a helper function so that we can call from the view to rearrange or reformat the output that we are calling from the controller. And problem resolved.
def logged_in_user_info
#user = User.find(1)
respond_to do |format|
format.html
format.json do
render :json => custom_json_for_user(#user)
end
end
end
private
def custom_json_for_user(user_data)
user_list =
{ :id => user_data.id,
:first_name => user_data.first_name.to_s,
:last_name => user_data.last_name.to_s,
:date_of_birth => user_data.date_of_birth.to_s,
:username => user_data.username,
:primary_email => user_data.primary_email,
:secondary_email => user_data.secondary_email,
:gender => user_data.gender
}
user_list.to_json
end

How do I set the default format, like JSON, for a customized Devise controller in Rails 3?

My Rails 3 app has three customized controllers for my Devise + OmniAuth integration. I needed to override the standard methods, like 'new', for user registrations and sessions. I specifically needed the controller methods to handle redirects and responses that are compatible with JSON formatting.
In my routes.rb file I have the following:
devise_for :users, :controllers => {
:omniauth_callbacks => "users/omniauth_callbacks",
:registrations => "users/registrations",
:sessions => "users/sessions"
}
That works as expected. My routes now show the custom controller routes like:
new_user_session GET /users/sign_in(.:format) {
:action =>"new",
:controller =>"users/sessions"
}
new_user_registration GET /users/sign_up(.:format) {
:action=>"new",
:controller=>"users/registrations"
}
To set the default format for a resource I would do something like this:
resources :users, :defaults => {
:format => 'json'
}
So, I tried this:
namespace "users" do
resources :registrations, :defaults => {
:format => 'json' }
resources :sessions, :defaults => {
:format => 'json' }
end
Which did not work as expected. I ended up with these routes:
new_users_registration GET /users/registrations/new(.:format) {
:format=>"json",
:action=>"new",
:controller=>"users/registrations"
}
new_users_session GET /users/sessions/new(.:format) {
:format=>"json",
:action=>"new",
:controller=>"users/sessions"
}
In order for this to work with my custom overrides in Devise, I need to format 'new_user_registration' not 'new_users_registration'.
I checked the 'devise_for' method and it does not have a :defaults option. I can use the 'devise_scope' method to set the individual routes, but that seems far less concise that the :defaults idiom.
Does anyone know of any routing magic that I can use to make this happen?
I found an answer that isn't necessarily satisfying, but it works. I tried this in routes.rb:
devise_scope :user do
get "sign_up", :to => "users/registrations#new",
:defaults => { :format => 'json' }
end
And I tried this in my custom controllers:
redirect_to new_user_registration_url, :format => 'json'
Neither worked. I am guessing both of those were incorrect in implementation. I finally used this in my custom controllers:
redirect_to :controller => 'users/registrations',
:action => 'new',
:format => 'json'
That replaced everywhere I originally had:
redirect_to new_user_registration_url
It's more verbose than I like and not very DRY, but it works.