Trying get a handle on oAuth by recreating twitter using the omniauth-twitter and twitter gems, and I'm stuck on viewing the tweets of all the users I'm following in my app together in one place. I can view my own tweets and other users' in their own profile fine.
This is the error trace
NoMethodError in PagesController#home
undefined method `where' for Twitter::Timeline:Class
app/models/twitter/timeline.rb:47:in `from_users_followed_by'
app/models/user.rb:24:in `hub'
app/controllers/pages_controller.rb:5:in `home'
pages_controller.rb
def home
if signed_in?
#hub_items = current_user.hub.paginate(page: params[:page])
end
end
user.rb
def hub
Twitter::Timeline.from_users_followed_by(self)
end
timeline.rb
module Twitter
class Timeline
def self.from_users_followed_by(user)
followed_user_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id"
Twitter::Timeline.where("user_id IN (#{followed_user_ids}) OR user_id = :user_id",
user_id: user.id)
end
end
end
** Controllers
hub_controller.rb
def show
#hub_items = []
render 'pages/home'
end
** Views
_hub.html.erb
<% if #hub_items.any? %>
<ol class="microposts">
<%= render partial: 'shared/hub_item', collection: #hub_items %>
</ol>
<%= will_paginate #hub_items %>
<% end %>
_hub_items.html.erb
<li id="<%= hub_item.id %>">
<% if #hub_item.any? %>
<% #hub_item.each do |post| %>
<% if post.provider == "twitter" %>
<%= render partial: "users/twitter_post", locals: {post: post} %>
<% end %>
</li>
So, Is there a way to write my self.from_users_followed_by method so that it doesn't have to inherit from ActiveRecord::Base? I can give anything else that's needed to figure this out.
Related
I want to upload an image to my localhost using gem 'carrierwave', i have painting and galleries controller like below code
painting controller
class PaintingsController < ApplicationController
def index
#paintings=Painting.all
end
def new
#painting=Painting.new
end
def show
#painting=Painting.find(params[:id])
end
def create
#byebug
#painting=Painting.new(painting_params)
if #painting.save
flash[:success]="Created image in album"
redirect_to gallery_path(#painting)
else
flash[:error]="Fail!"
render 'new'
end
end
private
def painting_params
params.require(:painting).permit(:name,:gallery_id)
end
end
Gallery controller
class GalleriesController < ApplicationController
def index
#galleries=Gallery.all
end
def new
#gallery=Gallery.new
end
def show
#gallery=Gallery.find(params[:id])
end
def create
#gallery=Gallery.create!(gallery_params)
redirect_to galleries_path
end
private
def gallery_params
params.require(:gallery).permit(:name)
end
end
ok then 2 model files :
gallery.rb
class Gallery < ApplicationRecord
has_many :paintings
end
painting.rb
class Painting < ApplicationRecord
def access_params
params.require(:painting).permit(:gallery_id, :name, :image)
end
belongs_to :gallery, optional: true
mount_uploader :image, ImageUploader
end
It seems everying goes well but then i stuck at the step showing image on show.html.erb in gallery.
show.html.erb
<div id="paintings">
<% #gallery.paintings.each do |painting| %>
<div class="painting">
<%= image_tag painting.image_url.to_s %>
<div class="name"><%= painting.name %></div>
<div class="actions">
<%= link_to "edit", edit_painting_path(painting) %> |
<%= link_to "remove", painting, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>
<div class="clear"></div>
</div>
the image isn't showed up althought flash in gallery 's controller reported that i created the image,i inspected the website then i tried print painting s'attributes on show.html.erb
<%= #gallery.name %>
<%= #gallery.paintings.name %>
<%= #gallery.paintings.gallery_id%>
<%= #gallery.paintings.image%>
Only gallery's name and painting's name are printed out. other two methods has an error occur.
undefined method `gallery_id' for #<ActiveRecord::Associations::CollectionProxy []>
I don't know why gallery can only access to painting's name but not others two.I searched for this error but i dont think those situation apply to mine . What is the problem guys?
Your "paintings" is a collection, not a single image, so you need to either iterate on each of them or select the first one:
<%= #gallery.paintings.first.name %>
<%= #gallery.paintings.first.gallery_id %>
<%= #gallery.paintings.first.image %>
I found my error ! in user controller add :image to method painting_params
def painting_params
params.require(:painting).permit(:name,:gallery_id,:image)
end
I'm trying to make a basic search function for a simple product inventory app on Sinatra, but don't know how to make the controller and view to properly output all the products which have similar names to a results page.
SearchPage.erb:
<form action="/search", method="post">
<input type="text" name="product[name]">
Controller:
post '/search' do
#Products = Product.find_by(name: params[:product][:name])
#Products = Product.all(:name.like => "%#{params[:name]}%") #found this on another question
erb :"result"
end
Result.erb
<% #Products.each do |product| %>
<%=product.name %>
<%=product.details %>
EDIT: I was able to make search work based on the suggestion with the following code. Thanks!:
Search.erb View
<form action="/search", method="get">
<input type="text" name="search">
Controller
get '/search' do
#products = Product.all
if params[:search]
#products = Product.search(params[:search])
else
#products = Product.all
end
erb :'results'
end
Model
class Product < ActiveRecord::Base
def self.search(search)
where("name like ?", "%#{search}%")
end
Results.erb View
<% if #products.present? %>
<table>
<td>Product Name</td><td>Company</td>
<% #products.each do |product| %>
<tr><td><%=h product.name %> </td>
<td><%=h product.company.name %></td>
<% end %>
<% else %>
<p>There are no Products containing the term(s) <%= params[:search] %>.</p>
<% end %>
</table>
I notice off the bat you're using a POST method. There is an easier way to do create search functionality for your products. Try this:
Posts Controller:
#products = Product.all
if params[:search]
#products = Product.search(params[:search]).order("created_at DESC")
else
#products = Product.all.order('created_at DESC')
end
end
Posts Model (note: If you are using SQLite keep it as LIKE. If you are using Postgres, change LIKE to ILIKE)
def self.search(search)
where('name like :pat or content like :pat', :pat => "%#{search}%")
end
Search Form (Put into your Result.erb and edit as needed but keep as get method. I personally like using form helpers but you can create a normal form if you'd like)
<%= form_tag(products_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Posts" %>
<%= submit_tag "Search" %>
<% end %>
Render Results
<% if #products.present? %>
<%= render #products %>
<% else %>
<p>There are no posts containing the term(s) <%= params[:search] %>.</p>
<% end %>
Let me know if this works for you. If not, i'll try help some more.
I have a form to fill in booking details, but I'm getting the following error:
NoMethodError in Bookings#new
undefined method `bookings_path'
This happened after change the routes.rb file, to nest the booking resources in the user resource.
My form file code is the following:
<% provide(:title, 'Book Now!') %>
<section id="book-now">
<%= form_for(#booking) do |f| %>
<header>
<h1>Edit booking</h1>
</header>
<%= f.text_field :name, placeholder: "Name" %>
<%= f.text_field :check_in, placeholder: "Check-in" %>
<%= f.text_field :check_out, placeholder: "Check-out" %>
<%= f.submit "Save" %>
<% end %>
</section>
My booking controller code is:
class BookingsController < ApplicationController
def new
#booking = Booking.new
end
def create
#booking = Booking.new(params_bookings)
#booking.user_id ||= current_user.id
if #booking.save
redirect_to user_path(#booking.user_id)
else
# render 'new'
end
end
end
private
def params_bookings
params.require(:booking).permit(:check_in, :check_out, :name, :user_id)
end
end
and my routes.rb file looks like this:
Hightide::Application.routes.draw do
resources :users do
resources :bookings
end
match '/users/:user_id/bookings/new', to: 'bookings#new', via: [:post, :get]
you have this error because your bookings routes are nested to the users, so when you write this
form_for #booking
you essentially call
form_for bookings_path, ...
coz rails gets the type of the object you send to form_for and tries to get vanilla path for it.
to solve the problem you need either create the vanilla bookings resources in your routes, or specify both a user and a booking reference for the form_for call, like so
form_for [current_user, #booking]
I want to bring this code from my view html into a popup when a link is clicked
Is that possible in Ruby on Rails? I already have the pop up working but I'm wondering about the code to show just the comments:
<div class = "comments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= image_tag("http://www.gravatar.com/someavatarlink %) <!-- Retrieves Gravatar -->
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
Added Ajax call to _comment_form.html.erb
<%= link_to "Link", comment, :remote => true %>
Comments
<% end %></div></div>
<div id ="modal" class = "comments"><% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %></div>
Added def show into comments controller
class CommentsController < ApplicationController
def new
#post = post.new(params[:post])
end
def show
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
def create
#post = post.find(params[:micropost_id])
#comment = Comment.new(params[:comment])
#comment.post = #post
#comment.user = current_user
if #comment.save
redirect_to(:back)
else
render 'shared/_comment_form'
end
end
end
Created show.erb.js and put it into 'comments' and 'shared' folders
$("#popup").html('<%= escape_javascript(render "comments") %>');
Then finally wrote my partial which is in comments/_comment.html.erb
<% if post.comments.exists? %>
<% post.comments.each do |comment| %>
<%= link_to comment.user.name, comment.user %>
<span class="timestamp"><%= time_ago_in_words(comment.created_at) %> ago</span>
<span class="content2"><%= comment.comment_content %></span>
<% end %>
<% end %>
1. Ajax call
To retrieve the data you use an Ajax call.
<%= link_to "Link", comment, :remote => true %>
Rails will evaluate these requests and will look for a .js view first (it will use .html if it does not exist).
Make also sure that the controller accepts requests to .js like
def show
#comment = Comment.find(params[:id])
respond_to do |format|
format.js
end
end
2. write js view
Add a show.erb.js view to your Comments . This is a JavaScript file with ERB evaluation.
In this template use your js popup code and tell it to fill a div with your html code like so:
$("#popup").html('<%= escape_javascript(render #comment) %>');
This will render the comment. The only thing we need then is a partial to render the html of the comment.
3. write partial for html
Write a partial for the view part you want to have in the popup. This can then be used in a normal html view or the js view. To make it work with the code above call it _comment.html.erb
To know more about partials you can check the guides here:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
I've created a simple memo feature for my app, and have successfully implemented in-place editing using the best_in_place gem.
The problem I am having is creating new memos using in-place editing, the following code returns undefined methodmemo' for nil:NilClass`
#partial for logged in users on homepage
<% provide(:title, current_user.name) %>
<h1>Hey
<%= current_user.name %>
</h1>
<div class="row-fluid">
<div class="span12">
<div class="row-fluid">
<div class="span6">
<h2>Memos</h2>
<ul id="memos" data-update-url="<%= sort_memos_url %>">
<%# render #memos %>
<% #memos.each do |memo| %>
<%= content_tag_for :li, memo do %>
<%= best_in_place memo, :memo %>
<% end %>
<% end %>
</ul>
<p><%# link_to "Add a memo", new_memo_path %></p>
<p><%= best_in_place #memo, :memo, :path => new_memo_path, :nil => "Add a memo" %></p>
</div>
<div class="span6"><h2>Coming up...</h2></div>
</div>
</div>
</div>
#memos controller
def index
#memos = Memo.order("position")
end
def show
#memo = Memo.find(params[:id])
end
def new
#memo = Memo.new
end
def create
#memo = Memo.new(params[:memo])
if #memo.save
redirect_to #memo, notice: "Successfully created memo."
else
render :new
end
end
def edit
#memo = Memo.find(params[:id])
end
def update
#memo = Memo.find(params[:id])
#memo.update_attributes(params[:memo])
respond_with #memo
end
def sort
params[:memo].each_with_index do |id, index|
Memo.update_all({position: index+1}, {id: id})
end
render nothing: true
end
#home page controller
respond_to :html, :json
def home
if signed_in?
#memos = Memo.order("position")
end
end
def sort
params[:memo].each_with_index do |id, index|
Memo.update_all({position: index+1}, {id: id})
end
render nothing: true
end
def help
end
end
I am new to rails and programming in general so I am sure it is a simple fix, none-the-less it's one that I cannot seem to remedy.
Have a look at (see 3.3.4):
http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials
Using partials and variables, means you need to stick to a certain naming convention. If your not doing that, then you need to send the variable (from the calling view) with it.
<%= render :partial => "form", :locals => { :zone => #zone } %>