I would like my users to be able to paginate through their own posts (scheduling emails). Thanks in advance to anyone who can help or steer me in the right direction!
Emails have different categories, Scheduled and Pending. I have two scopes for these in my emails model.
email.rb
scope :pending, -> { where(email_pending: true) }
scope :approved, -> { where(email_pending: false) }
I'm using devise to organize users. In my Users controller I have users#show action and I'm pulling the user's emails like this.
users_controller.rb
def show
#emails = #user.emails
end
Then in my view I list each category of email like this.
users#show
<% #user.emails.approved.each do |email| %>
-----Stuff------
<% end %>
Same thing goes for pending.
So, to paginate (using will_paginate) I tried.
users_controller.rb
#emails = #user.emails.approved.paginate(page: params[:page], per_page: 5)
and in the view I added.
users#show
<%= will_paginate #user.emails.approved %>
I also tried a few other things, but it was pretty much just guessing and trial and error... nothing worked.
Thanks again for any help! Please let me know if anyone would needs more information from me.
-Kraymer
if in the show method of your users_controller you have
#emails = #user.emails.approved.paginate(page: params[:page], per_page:
in the view you will need to use the #emails that you created
<% #emails.each do |email| %>
.....
<% end %>
<%= will_paginate #emails %>
Since by accessing #user.emails.approved you are not looking at paginated version, but the original active relation.
Related
I'm somewhat new to rails. I'm going through making the classic twitter clone right now. I want to have a search bar on my homepage that allows the user to search for a twitter handle, and if the handle exists, it will send the user to the show page for that twitter handle.
I've been following a RailsCast on how to implement a simple search, but instead of doing it on the index like the video, I want to do it on the show action. I've run into some problems though. The form sits on my user index view.
Here is the error:
ActionController::UrlGenerationError in Users#index
Showing c:/Sites/Projects/twitterapp/twitter/app/views/users/index.html.erb where line #2 raised:
No route matches {:action=>"show", :controller=>"users"} missing required keys: [:id]
Here is the form:
<%= form_tag(user_path, method: 'get') do %>
<%= text_field_tag(:search, params[:search]) %>
<%= submit_tag("Search", name: nil) %>
<% end %>
Here is my show action:
def show
#user = User.search(params[:search])
end
And here is my search method in my user model:
def self.search(search)
if search
find(:all, conditions:['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
Actually you cannot use the show method as a search result finder. Because according to the rails convention:
For any resource like users, rails scaffold generates index,new, show, create, update, delete methods based on your routes files.
Thus based on the conventional way, show method always asks for an object. Lets say you are using UserContoller show method. It asks for a user object. Which you haven't provide in the form. that's why :id missing error is given.
I would tell you to do some more learning. And for searching create a different method in a different controller and define that controller method to the routes.rb file. This is the best way to do.
If you still want to use the show method, then change the show methods routing from the routes.rb file. You've to manually declare the show action on routes file.
you are using user_path and path need to inform id from present user
you can do this in action :index but I recommend you to create a action to this
view
<%= form_tag(search_users_path, method: 'get') do %>
<%= text_field_tag(:search, params[:search]) %>
<%= submit_tag("Search", name: nil) %>
<% end %>
routes.rb
resources :users do
post 'search', :on => :collection
end
users_controller.rb
def search
#user = User.search(params[:search])
end
You should to create a view search.html.erb similar as index.html.erb
As Emu and Breno pointed what causing the problem user_path requires an user id
Solution idea:
Why not just point to users index action? like this:
<%= form_tag(users_path, method: 'get') do %>
<%= text_field_tag(:search, params[:search]) %>
<%= submit_tag("Search", name: nil) %>
<% end %>
users_controller.rb:
def index
if params[:search]
#user = User.search(params[:search])
end
end
and you can use ajax remote: true to handle the returned user object
Found your question via Google, but the responses and suggestions didn't work for me. Found another solution that did, so seems worth posting here.
"Search and Filter Rails Models Without Bloating Your Controller":
http://www.justinweiss.com/articles/search-and-filter-rails-models-without-bloating-your-controller/
I have a rails app that is working fine using acts_as_votable. The like button upvotes the post count, and then switches to an un-like button and this down votes the post count.
My issue is, that since I started using the Public Activity gem, I can't find a way to remove likes from the feed. I have used the following loop in the activities index view:
<% #activities.each do |activity| %>
<p>
<% if activity.trackable %>
<%= link_to activity.owner.name, activity.owner %>
<%= render_activity activity %>
<% end %>
</p>
<% end %>
When I delete a comment, the entire line in the activity feed of 'FOO added a comment on BAR' disappears. However, because the acts as votable gem actually creates a downvote rather than destroying the upvote, the line 'FOO liked BAR' still appears and would be subsequently followed by 'FOO unliked BAR'.
Does anybody know how I can locate the upvote by the current_user on a particular post and then destroy it?
Below is my controller code for like and unlike as it stands:
def like
#ink.create_activity :like, owner: current_user
#ink.upvote_by current_user
redirect_to :back
end
def unlike
#ink.downvote_by current_user
redirect_to :back
end
Thanks
I know this have been answered but the ideal and easiest answer would be using the official gem method which is unvote_by it works for both upvotes and downvotes.
It looks like what you want to do is remove the model's like notification when it is "unliked" - is that correct? All you have to do is find the relevant activity and destroy it. Because Activities are models just like any other, you can do this with destroy and/or destroy_all.
I'm not quite sure what your public_activity model looks like, so instead of giving a specific example I'll link you to this post on the mailing list for the gem, which shows examples of deleting public activity records.
You may also find it useful to delete the record by its trackable_id - for example:
#activity = PublicActivity::Activity.find_by(trackable_id: (params[:id]), trackable_type: controller_path.classify)
#activity.destroy
There's more information about how that works in this SO answer.
For anyone that stumbles across this in future, thanks to Element119 I was eventually able to pinpoint the current users likes on a particular post with a few variables and arel searches. I then destroyed the pinpointed likes.
def like
#post.create_activity :like,
owner: current_user,
recipient: #post.user
#post.upvote_by current_user
redirect_to :back
end
def unlike
#post.downvote_by current_user
#currentUserLikes = PublicActivity::Activity.where(trackable_id: #post.id, owner_id: current_user.id, key: "post.like")
#currentUserLikes.destroy_all
redirect_to :back
end
I am new to Ruby on rails. I've created basic demo apps by tutorial learning by examples.
Application have three model User,Village and article
Village has many users, Village has many articles, user and article belongs to village
I work , migration work fine
when iam in http://0.0.0.0:3000/villages/1 , i display all user that belong to village 1
My question is how display in all user in village one this url http://0.0.0.0:3000/villages/1/users
To do that you need to add the url to the routes.rb file under the config folder.
Add line like below
resources :villages do
member do
get '/user', to: 'villages#show'
end
end
I am assuming that your villages show action is the one that has all the user details displayed.
routes.rb
resources :villages do
member do
get :users, :articles
end
end
In villages_controller
def users
#village = Village.find(params[:id])
#values = #village.users.paginate(page: params[:page])
render 'show_data'
end
def articles
#village = Village.find(params[:id])
#values = #village.articles.paginate(page: params[:page])
render 'show_data'
end
In show_data.html.erb
<% if #values.any? %>
<% #values.each do |value| %>
<%= value.name %>
<% end %>
<%end%>
<%= will_paginate %>
I'm trying to display products in my ProductsController using the following action:
def index
#products = Products.find(params[:id])
end
And my index.html.erb:
<% #products.each do |product| %>
<p>
<b>Product:</b>
<%= product.name %>
</p>
<% end %>
However I'm getting the following error:
ActiveRecord::RecordNotFound (Couldn't find Products without an ID)
I'm kind of new to rails so I'm not sure what I'm doing wrong.
You should really be more careful with the language you are using to describe what you're trying to do. Being loose and sloppy with it has an impact both on how other people understand you as well as how you think about what you're doing.
That said, you're using an index action, which is used to list or provide an index of records, but then you're trying to get a single product (find(id)). These things are incongruent. Even your variable name is incongruent with the data you're loading.
def index
#products = Product.all
end
def show
#product = Product.find(params[:id])
end
In the future, you should read a little more closely and read the error messages you're getting (this one straight up tells you that params doesn't have an id for this action, cluing you into what's going on).
Apologies in advance, I am a newbie trying to get my head around rails.
My View at the bottom works when I use:
def show
#posts = Post.all
end
However in my controller I now have:
def show
#posts = Post.find_by_category_id params[:id];
end
In my view I have
<%= #posts.each do |post| %>
<%= post.title %>
<% end %>
Some please explain why I get this error. What should I use. category_id is a foreign key on the Post table.
Look at http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
Finds the first record matching the specified conditions
find_by_ will return only one post, not a collection. So you are not able to use each.
try
def show
#posts = Post.all.find_by_category_id params[:id];
end