I'd like to find out if the current user is friends with the root node user, using Koala, and if so, print a statement saying: you are friends.
My problem is the parsing of data returned from facebook.
user.rb
def facebook
#facebook ||= Koala::Facebook::API.new(oauth_token)
block_given? ? yield(#facebook) : #facebook
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil # or consider a custom null object
end
def facebook_friend
facebook.get_connection("me", "friends/#user.user_id")
end
Profile.html.erb
<% if current_user.facebook_friend.include?(#user.uid)? %>
<p> you are friends</p>
<% else %>
<p> you are not friends</p>
<% end %>
Output:
<%= current_user.facebook_friend %>
will return: [{"name"=>"Elizabeth", "id"=>"100008217009369"}]
<%= #user.uid %>
will return: 100008217009369
Thanks!!!
It was much easier than I thought:
In the user model simple used the Koala method:
def facebook_friend(user)
facebook.get_connection("me", "friends/#{user.uid}")
end
def my_friends_list
facebook.get_connection("me", "friends")
end
Then in the Profile View, adding current_user and #user as arguments:
<% if #user.uid.present? %>
<% if signed_in? && current_user == #user %>
<p><%= current_user.my_friends_list.count %> friends</p>
<% elsif current_user.facebook_friend(#user).empty? %>
<p><%= current_user.mutual_friends.count %> friends in common</p>
<% else %>
<p>You are Facebook friends</p>
<% end %>
<% else %>
<p>Not connected</p>
<% end %>
Related
I have three models: Lesson, Questions and Answers.
What I'm trying to do is on the show lesson view, display the questions and allow users to create answers for each answer. However, I'm not sure the best way to do this.
I tried this approach on my lesson#showview:
<% #questions.each do |question| %>
<%= question.content %><br /><br />
<%= simple_form_for :answers do |f| %>
<%= f.input :content %>
<%= f.hidden_field :question_id, :value => question.id %>
<%= f.button :submit %>
<% end %>
<% end %>
With this code, I receive the error param is missing or the value is empty: lesson
Answer has two fields: content, question_id.
My other concern is that I'd like to have this be user friendly, so if there are multiple questions, there should be multiple input boxes for the answers (one per each question) and one submit button (so multiple answers can be posted at one time).
I think that my approach my bad, but I'm not sure how else to do this, so any help would be greatly appreciated.
Here's what I have so far:
Models:
class Lesson < ActiveRecord::Base
has_many :questions, dependent: :destroy
has_many :answers, through: :questions
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :answers, reject_if: :all_blank, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :lesson
has_many :answers, dependent: :destroy
end
class Answer < ActiveRecord::Base
belongs_to :question
end
Lessons Controller
class LessonsController < ApplicationController
def show
#questions = #lesson.questions
end
# PATCH/PUT /lessons/1
# PATCH/PUT /lessons/1.json
def update
respond_to do |format|
if #lesson.update(lesson_params)
format.html { redirect_to #lesson, notice: 'Lesson was successfully updated.' }
format.json { render :show, status: :ok, location: #lesson }
else
format.html { render :edit }
format.json { render json: #lesson.errors, status: :unprocessable_entity }
end
end
end
private
def lesson_params
params.require(:lesson).permit(:name,
answers_attributes: [:id, :content, :question_id]
)
end
end
routes.rb
resources :lessons
post '/lessons/:id', to: "lessons#update"
Add gem in Gemfile and run bundle install:-
gem "nested_form"
On lession show page:-
<%= nested_form_for #lession do |lession_form| %>
<%= #lession.content %>
<%= lession_form.fields_for :questions do |question_form| %>
<% #questions.each do |question| %>
<%= question.content %><br /><br />
<%= question_form.fields_for :answers do |answer_form| %>
<%= answer_form.text_field :content %>
<%= answer_form.link_to_remove "Remove this answer" %>
<% end %>
<%= question_form.link_to_add "Add more answer", :answers %>
<% end %>
<% end %>
<%= lession_form.submit 'Update' %>
<% end %>
I would have thought you are able to achieve without the use of a Gem.
You may need to specify inverse_of in your model. I have previously found this was required in nested attributes when dealing with forms.
class Question < ActiveRecord::Base
belongs_to :lesson
has_many :answers, dependent: :destroy, :inverse_of => :question
end
class Lesson < ActiveRecord::Base
has_many :questions, dependent: :destroy, :inverse_of => :lessons
has_many :answers, through: :questions
#etc.
end
In your lessons controller:
def show
#lesson = Question.find(params[:id])
#questions = #lesson.questions
x.times { #lesson.questions.answer.build }
end
In your views/lessons/show page:
<%= form_for #lesson do |lesson| %>
<%= #lesson.whatever_attribute %>
<%= lesson.fields_for :questions do |question| %>
<% #questions.each do |question| %>
<%= question.content %>
<% end %>
<div id="answers-div" class='form-group'>
<%= question_form.fields_for :answers do |answer| %>
<%= answer.text_field :content id:"answer-entry" %>
<% end %>
</div>
<% end %>
<% end %>
<%= lesson.submit 'Submit' %>
Below the form add some buttons to add further answers or remove:
<button class="btn btn-default" id="addNewAnswer">Add Another Answer Box</button><br>
<button class="btn btn-default" id="deleteNewAnswer">Delete Last Answer</button>
You can then add & remove answers on the fly with jQuery.
$(document).ready(function(){
$("#addNewAnswer").click(function() {
$("#answers-div").append(createNewInputElement($("#answers-div")));
});
});
function createNewInputElement(form) {
var newIndex = $("#answers-div").children('input#choice-entry').length;
var newInput = $("#answer-entry").clone().attr('name', generateNewInputName(newIndex));
newInput.val('');
return newInput;
};
function generateNewInputName(idx) {
return "question[answers_attributes][" + idx + "][content]"
};
$(document).ready(function(){
$("#deleteNewAnswer").click(function() {
if ($("#answers-div input").length > 1) {
$("#answers-div input:last-child").remove();
}
});
});
The use of a nested form is not an issue. You require a nested form to allow you to nest answers within your lessson.questions but you are only allowing the user to give input
If using Rails 4 (and the Strong params) you will also need to allow these with something along these lines (otherwise the params being passed will not be allowed through).
private (in your Lessons controller)
def lesson_params
params.require(:lesson).permit(:content, answer_attributes:[:content])
end
This may not be perfect but it's the start of some sort of solution to your question I would hope.
Try to testing this code, an email me again
in your lesson#show.html.erb
<% for question in #lesson.questions %>
<%= question.content %><br /><br />
<%= simple_form_for :answers do |f| %>
<%= f.input :content %>
<%= f.hidden_field :question_id, :value => question.id %>
<%= f.button :submit %>
<% end %>
<% end %>
I have created a view for volunteers to sign up for specific shifts. I am able to write my view and controller for shifts so that, when the form_for submit button is clicked, the current_user.id is pushed onto the specific shift's user_ids attribute, which is an array. The problem is that it updates each of the shift's user_ids with the current user's id. What am I missing here? Any insight would be appreciated. Thanks.
volunteer.html.erb
<div class="container">
<div style="width:60%;margin:0 auto 0 auto;" class="inner-lower">
<div class="list-group">
<% #uniq_shifts.each do |title| %>
<% #shifts_by_title = #shifts.where(title: title) %>
<% #title_vols = #shifts_by_title.pluck(:vols_needed).sum %>
<% #times = #shifts_by_title.pluck(:time) %>
<% #time_vols = #shifts_by_title.pluck(:vols_needed) %>
<% if #title_vols > 0 %>
<!-- ACTIVITY TITLE -->
<div id=<%= "activity#{title}" %> class="activity">
<a href="#" class="list-group-item">
<%= title %>
<!-- ACTIVITY NUMBER VOLUNTEERS NEEDED-->
<span class="badge-volunteer"><%= #title_vols %></span>
</a>
</div>
<div class="sub" style="display:none;">
<% #shifts_by_title.each do |shift| %>
<!-- ACTIVITY SHIFT -->
<a href="#" class="list-group-item-sub">
<!-- ACTIVITY SHIFT TIME -->
<%= shift.time %>
<span class="badge">
<!-- ACTIVITY SHIFT NUMBER OF VOLUNTEERS NEEDED -->
<%= shift.vols_needed %>
</span>
</a>
<%= form_for shift, :method => :put do |f| %>
<%= f.hidden_field :user_ids, :value => shift.add_user_id(#user.id) %>
<%= f.submit "sign up", class: "btn btn-primary" %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
</div>
</div>
shift.rb
class Shift < ActiveRecord::Base
has_and_belongs_to_many :users
def add_user_id(user_id)
user_ids_will_change!
update_attributes user_ids: self.user_ids + [ user_id ]
self.save
end
end
shifts.controller.rb
class ShiftsController < ApplicationController
before_action :set_shift, only: [:show, :edit, :update, :destroy]
before_action :volunteer, only: [:show, :edit, :update, :destroy]
def index
#shifts = Shift.all
end
def volunteer
#shifts = Shift.all
#user = current_user
#shift_titles = #shifts.pluck(:title)
#uniq_shifts = #shift_titles.uniq
#vols_needed = #shifts.pluck(:vols_needed)
unless current_user
render action: 'new'
end
end
def show
end
def new
#shift = Shift.new
end
def edit
end
def create
#shift = Shift.new(shift_params)
if #shift.save
redirect_to #shift, notice: 'Shift was successfully created.'
else
render :new
end
end
def update
#user = current_user
if #shift.update(shift_params)
redirect_to #shift, notice: 'Shift was successfully updated.'
else
render :edit
end
end
def destroy
#shift.destroy
redirect_to pages_url, notice: 'Shift was successfully destroyed.'
end
private
# Use callbacks to share common setup or contraints between actions.
def set_shift
#shift = Shift.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shift_params
params.require(:shift).permit(:title, :time, :vols_needed, :user_ids => [])
end
end
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 %>
I've been stuck on this problem for a couple of days now.
I've have some success with Railscasts Episode #198, but that one is for Rails 2. There have been some changes in Rails 3 that make it so the code provided in Episode #198 won't work.
The problem lies within the edit_individual.html.erb:
Original Code (provided by Ryan # Railscasts):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% fields_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
Modified Code (simply changed fields_for to form_for):
<% form_tag update_individual_products_path, :method => :put do %>
<% for product in #products %>
<% form_for "products[]", product do |f| %>
<h2><%=h product.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
In the new code, each record is placed within a form of their own, all inside one single form (which is the one I only want).
My question is, how can I get the code provided by Railscasts Episode #198 to work in Rails 3?
Here is a link to the Railscast I mentioned:
http://railscasts.com/episodes/198-edit-multiple-individually
Thank You,
c.allen.rosario
I found the solution. Just need to modify the following line in the code provided by Ryan # Railscasts:
<% fields_for "products[]", product do |f| %>
and change it to:
<%= fields_for "products[]", product do |f| %>
Notice, that the <% has been modified to <%=.
final solution:
<% form_tag update_individual_products_path :method => :put do %>
<% for product in #products %>
<%= fields_for "products[]", product do |f| %>
<h2><%= h product.name %></h2>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
I was wondering if anyone could explain this solution to me. From what I understand you should only need a <% in front of the fields_for.
c.allen.rosario
The change in Rails 3 from <% fields_for to <%= fields_for is because it was confusing that form_for, form_tag, etc... were using <% form... %> even though they they were outputting html code.
With Rails 3, since they output html code, they use <%=.
Please note that your first line is deprecated:
<% form_tag update_individual_products_path, :method => :put do %>
should be
<%= form_tag update_individual_products_path, :method => :put do %>
Same for all form tags.
Hi I have any form with nested form, for example
<% form_for :main do |f| %>
trying to insert code here
<% fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
And then I am trying to insert anything to a main form, nested form doesn't produce any output. It outputs only when it is the only object in main form. Any suggestions?
From the Rails 3 documentation examples you need to write your form_for like this:
<%= form_for :main do |f| %>
# trying to insert code here
<%= fields_for :nested do |nested_form| %>
<%= nested_form.text_field :description %>
<% end %>
<% end %>
Note the <%= for both form_for and fields_for