Acts_as_taggable gem. Cloud - ruby-on-rails

I have a list of categories. When you click on a category lets say Computers it should only show a tag cloud of the items in that category in this case Computers. Tags from the Mobile Phone category should not be showing.
Currently my tag cloud shows all tags across all categories.
Im using the Acts_as_tagable_gem https://github.com/mbleigh/acts-as-taggable-on
How would someone go about showing only the tags inside that category?
Here is my view. Please refer to tag_cloud
<p id="notice"><%= notice %></p>
<div class = "col-md-3">
<h1>
<strong><%= #category.name %></strong>
</h1>
</div>
<div class = "col-md-9">
<div id="tag_cloud">
<% tag_cloud Item.tag_counts, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name, id: #category.id), class: css_class %>
<% end %>
</div>
</div>
<div class = "col-md-12">
<div class="line-separator"></div>
</div>
<div class = "col-md-12">
<div id="items" class="transitions-enabled">
<% #items.each do |item| %>
<div class="box panel panel-default">
<div class="itemlisttitle"><%= item.title %></div>
<%= link_to image_tag(item.image.url (:medium)), item %>
<div class ="panel-body">
<div class = "itemlistprice">$<%= item.price %></div>
<div class = "itemlistretailer"><%= image_tag item.user.avatar(:thumb) %> Retailer: <%= link_to item.user.username, item.user %></div>
</div>
</div>
<% end %>
</div>
</div>
Routes Please refer to the last line
Rails.application.routes.draw do
resources :categories
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
resources :users
get 'user_items' => 'users#show_user_items'
root 'items#home'
get 'signup' => 'users#new'
get 'show' => 'users#show'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :items
get 'items_new' => 'items#new'
get 'tags/:tag', to: 'categories#show', as: :tag
Category Controller Please refer to show action
class CategoriesController < ApplicationController
before_action :set_category, only: [:show]
before_action :admin_user, only: [:destroy, :index, :edit]
def index
#categories = Category.all
end
def show
if params[:tag]
#items = Item.tagged_with(params[:tag])
else
#items = Item.where(category_id: #category.id).order("created_at DESC")
end
end
def new
#category = Category.new
end
def edit
end
def create
#category = Category.new(category_params)
respond_to do |format|
if #category.save
format.html { redirect_to #category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: #category }
else
format.html { render :new }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #category.update(category_params)
format.html { redirect_to #category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: #category }
else
format.html { render :edit }
format.json { render json: #category.errors, status: :unprocessable_entity }
end
end
end
def destroy
#category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_category
#category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:name, :parent_id)
end
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.try(:admin?)
end
end

Related

Although i got one i got NoMethodError message

In ruby on rails i want to show user's articles. Here is my articles_controller.rb
class ArticlesController < ApplicationController
before_action :set_article, only: %i[ show edit update destroy ]
before_action :authenticate_user!, except: [:index, :show ]
before_action :correct_user, only: [:edit, :update, :destroy]
# GET /articles or /articles.json
def index
#articles = Article.all
end
def myarticles
#articles = Article.all
end
# GET /articles/1 or /articles/1.json
def show
end
# GET /articles/new
def new
##article = Article.new
#article = current_user.article.build
end
# GET /articles/1/edit
def edit
end
# POST /articles or /articles.json
def create
##article = Article.new(article_params)
#article = current_user.article.build(article_params)
respond_to do |format|
if #article.save
format.html { redirect_to #article, notice: "Article was successfully created." }
format.json { render :show, status: :created, location: #article }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /articles/1 or /articles/1.json
def update
respond_to do |format|
if #article.update(article_params)
format.html { redirect_to #article, notice: "Article was successfully updated." }
format.json { render :show, status: :ok, location: #article }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# DELETE /articles/1 or /articles/1.json
def destroy
#article.destroy
respond_to do |format|
format.html { redirect_to articles_url, notice: "Article was successfully destroyed." }
format.json { head :no_content }
end
end
def correct_user
#article = current_user.article.find_by(id: params[:id])
#redirect_to friends_path, notice: "Not Authorized To Edit This Friend" if #friend.nil?
redirect_to articles_path, notice: "Not Authorized To Edit This Article" if #article.nil?
end
private
# Use callbacks to share common setup or constraints between actions.
def set_article
#article = Article.find(params[:id])
end
# Only allow a list of trusted parameters through.
def article_params
params.require(:article).permit(:title, :description, :paragraph, :image, :content, :user_id)
#params.require(:article).permit(:title, :description, :paragraph, :image, :content) eskisi
end
end
in views/articles i have myarticles.html.erb
and in routes i have get 'articles/myarticles'
in myarticles.html.erb i have the same code for show.html.erb(actually i want to show the articles that belongs to that user but i will add that later):
<p id="notice"><%= notice %></p>
<div style="justify-content: center;display: flex;" >
<div style="max-width:680px;width: 100%;" >
<%# <div class="container-{680px}"> %>
<br>
<h1>
<%= #article.title %>
</h1>
<p>
<%= #article.description %>
</p>
<%# <p style="justify-content: center;display: flex;" > %>
<p style="flex-direction: column;display: flex;" >
<%if #article.image.attached? %>
<%=image_tag #article.image ,class:"img-fluid"%>
<% end %>
</p>
<p>
<%= #article.content %>
</p>
<%#
<p>
<%= #article.paragraph %>
<%# </p> %>
<br>
<%= link_to 'Edit', edit_article_path(#article) ,class:"btn btn-secondary"%> |
<%= link_to 'Back', articles_path ,class:"btn btn-secondary"%>
</div>
</div>
But i got this message:
NoMethodError in Articles#myarticles
Showing C:/Users/oem/Desktop/blog/app/views/articles/myarticles.html.erb where line #8 raised:
undefined method `title' for nil:NilClass
<h1>
<%= #article.title %>
</h1>
In your controller actionmyarticles you declared #articles. But in the view you are using #article. #article is available in show where is declared with the before_action callback, but not in my articles.
You probably want to show a list of articles in myarticles view so you can do something like:
<%= #articles.each do |article| %>
<h1>
<%= article.title %>
</h1>
...and so on
<% end %>

Couldn't find ProjectSession with 'id'=

Here i have a project to which i am adding a session and for a project session i am trying to add task.
i am able to create project and add project session for project but when i am trying to add task for session using project_sessions_id i am getting error Couldn't find ProjectSession with 'id'= and 'app/controllers/tasks_controller.rb:60:in set_project_session i am able to get the project session id also project_sessions/11 in the url but when i click 'create task' i am getting this error. how can i resolve this?
here's what i have done
ProjectSessionController:
class ProjectSessionsController < ApplicationController
before_action :set_project_session, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :set_project
def index
#project_sessions = ProjectSession.all
end
def show
#project_sessions = ProjectSession.where(project_id: #project.id).order("created_at DESC")
end
def new
#project_session = ProjectSession.new
end
def edit
end
def create
#project_session = ProjectSession.new(project_session_params)
#project_session.project_id = #project.id
#respond_to do |format|
if #project_session.save
redirect_to #project
#format.html { redirect_to #project_session, notice: 'Project session was successfully created.' }
#format.json { render :show, status: :created, location: #project_session }
else
format.html { render :new }
format.json { render json: #project_session.errors, status: :unprocessable_entity }
end
#end
end
def update
respond_to do |format|
if #project_session.update(project_session_params)
format.html { redirect_to #project_session, notice: 'Project session was successfully updated.' }
format.json { render :show, status: :ok, location: #project_session }
else
format.html { render :edit }
format.json { render json: #project_session.errors, status: :unprocessable_entity }
end
end
end
def destroy
#project_session.destroy
respond_to do |format|
format.html { redirect_to project_sessions_url, notice: 'Project session was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_session
#project_session = ProjectSession.find(params[:id])
end
def set_project
#project = Project.find(params[:project_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_session_params
params.require(:project_session).permit(:session_date, :session_name, :session_description, :start_time, :end_time)
end
end
Task controller:
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :set_project_session
def index
#tasks = Task.all
end
def show
end
def new
#task = Task.new
end
def edit
end
def create
#task = Task.new(task_params)
#task.session_id = #project_session.id
respond_to do |format|
if #task.save
redirect_to #project_session
else
format.html { render :new }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #task.update(task_params)
format.html { redirect_to #task, notice: 'Task was successfully updated.' }
format.json { render :show, status: :ok, location: #task }
else
format.html { render :edit }
format.json { render json: #task.errors, status: :unprocessable_entity }
end
end
end
def destroy
#task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_task
#task = Task.find(params[:id])
end
def set_project_session
#project_session = ProjectSession.find(params[:project_session_id])
end
def task_params
params.require(:task).permit(:name, :description)
end
end
routes:
Rails.application.routes.draw do
get 'hr_dashboard/index'
resources :roles
resources :project_sessions
devise_for :users
resources :tasks
resources :projects do
resources :project_sessions, except: [:show, :index]
end
resources :project_sessions do
resources :tasks, except: [:show, :index]
end
authenticated :user do
root 'admindashboard#index', as:"authenticated_root"
end
root 'welcome#index'
get 'userdashboard/index'
get 'admindashboard/index'
get 'welcome/index'
end
View for creating new task
<div class="container">
<h1>New Task</h1>
<%= form_for(#task) do |f| %>
<% if #task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% #task.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', tasks_path %>
</div>
I figured it out!
I had forgotten to add #project_session in
<%= form_for([#project_session, #task]) do |f| %>
just added that and it worked.
In your create action of Task controller, you have to add:
#task= #project_session.tasks.build(task_params)
Because right now, you're not telling the task, to build from the project_session (or with respect to the project_session) you're just telling it to create a new task.
#task = Task.new
And in the routes.rb file, you've prepared for that to happen by nesting the resources, so it's currently looking for the ID of a task that belongs_to project_session. But can't find any.
And also, in your form_for element when creating a ProjectSssion you have let that form know which route it should belong to - I guess you could say - since you still have the un-nested resources available:
resources :tasks
If project_sessions shouldn't be creatable without a parent, there's no reasons for keeping that, so you should just remove it.
Anyways, here's what the form_for should look like:
<%= form_for([#project_session, #task]) do |f| %>

View Rails record Associations in modal

In my Rails app I have 3 models Kid, Classroom, and Teacher. Basically, I have a table in my view which lists all the classrooms, when I click on a button I want a modal to pop up that lists the teachers and students assigned to that classroom, as there is an association where a Classroom has_many Teachers and has_many Kids. I have the code ready for the modal, I have copied it below, however I am having a difficult time scoping a specific classroom object to the modal. All help is appreciated!
<div class="modal fade" id="showClassModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 id="showClassroomHeader"></h4>
</div>
<div class="modal-body">
<h4>Teachers Assigned</h4>
<ol>
<% #classroom.teachers.each do |teacher| %>
<li><%= teacher.first_name %> <%= teacher.last_name %></li>
<% end %>
</ol>
<br>
<h4>Students</h4>
<ol>
<% #classroom.kids.each do |kid| %>
<li><%= kid.first_name %> <%= kid.last_name %></li>
<% end %>
</ol>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<% if current_user.admin? %>
<button class="btn btn-warning" id="editClassroomBtn">Edit</button>
<% end %>
</div>
</div>
</div>
EDIT:
Classroom controller:
class ClassroomsController < ApplicationController
before_action :set_classroom, only: [:show, :edit, :update, :destroy]
# GET /classrooms
# GET /classrooms.json
def index
#user ||= current_user
if current_user.admin?
#classrooms = Classroom.all
else
#classrooms = #user.classrooms.all
end
end
# GET /classrooms/1
# GET /classrooms/1.json
def show
classroom = Classroom.find(params[:id])
render :text => classroom.class_desc
end
# GET /classrooms/new
def new
#classroom = Classroom.new
end
# GET /classrooms/1/edit
def edit
end
# POST /classrooms
# POST /classrooms.json
def create
#classroom = Classroom.new(classroom_params)
classroom.user = current_user
respond_to do |format|
if #classroom.save
format.html { redirect_to root_url, notice: 'Classroom was successfully created.' }
format.json { render :show, status: :created, location: #classroom }
else
format.html { render :new }
format.json { render json: #classroom.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /classrooms/1
# PATCH/PUT /classrooms/1.json
def update
respond_to do |format|
if #classroom.update(classroom_params)
format.html { redirect_to root_url, notice: 'Classroom was successfully updated.' }
format.json { render :show, status: :ok, location: #classroom }
else
format.html { render :edit }
format.json { render json: #classroom.errors, status: :unprocessable_entity }
end
end
end
# DELETE /classrooms/1
# DELETE /classrooms/1.json
def destroy
#classroom.destroy
respond_to do |format|
format.html { redirect_to classrooms_url, notice: 'Classroom was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_classroom
#classroom = Classroom.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def classroom_params
params.require(:classroom).permit(:class_name, :class_desc, :capacity, :start_range, :end_range)
end
end
Routes.rb:
Rails.application.routes.draw do
resources :rationales
resources :teachers
resources :classrooms
resources :kids
devise_for :users, :skip => [:sessions]
as :user do
get 'login' => "devise/sessions#new", :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
delete 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
root :to => 'kids#index'
resources :kids do
get 'discharge', on: :member
end
resources :kids do
get 'restore', on: :member
end
end
As mentioned in the comments section above, adding the modal inside <% #classrooms.each do |classroom| %> did the trick!

Rails, edit link will redirect but update button won't work

In my rails app I have this edit form that does not seem to work properly. If I enter the edit-site through any link, everything will look fine but the form submit button will not work. If I refresh the window it works fine.
Edit.html.erb
<div class="alien-choice">
<h2 class="player_name"><%= current_user.username %></h2>
<p> Choose <span>0</span>/2 Power</p>
<%= form_for [#gameround, #currentplayer] do |f| %>
<% alleflares = #currentplayer.flares %>
<% alleflares.each { |val|
printAlien = Alien.find_by_title(val)
%>
<div class="alien_wrap">
<h3><%= printAlien.title %> <span>[<%= printAlien.expansion.abbr %>]</span></h3>
<label for="<%= printAlien.title %>">
<div class="alien" style="background-image: url(<%= printAlien.avatar.url %>)">
</label>
<%= f.check_box(:aliens, { :multiple => true, :id => printAlien.title, :class => 'choosealiens'}, printAlien.title, nil) %>
</div>
</div>
<% } %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', gameround_currentplayers_path %>
The controller
class CurrentplayersController < ApplicationController
before_action :set_currentplayer, only: [:show, :edit, :update]
# GET /currentplayers
# GET /currentplayers.json
def index
#currentplayers = Currentplayer.all
end
# GET /currentplayers/1
# GET /currentplayers/1.json
def show
#gameround = Gameround.find(params[:gameround_id])
#currentplayer = #gameround.currentplayers.find(params[:id])
current_user
end
# GET /currentplayers/new
def new
#gameround = Gameround.find(params[:gameround_id])
#currentplayer = Currentplayer.new
end
# GET /currentplayers/1/edit
def edit
#gameround = Gameround.find(params[:gameround_id])
#currentplayer = #gameround.currentplayers.find(params[:id])
end
# POST /currentplayers
# POST /currentplayers.json
def create
#gameround = Gameround.find(params[:gameround_id])
#currentplayer = #gameround.currentplayers.create(currentplayer_params);
respond_to do |format|
if #currentplayer.save
format.html { redirect_to #gameround, notice: 'Currentplayer was successfully created.' }
format.json { render :show, status: :created, location: #currentplayer }
else
format.html { render :new }
format.json { render json: #currentplayer.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /currentplayers/1
# PATCH/PUT /currentplayers/1.json
def update
#gameround = Gameround.find(params[:gameround_id])
#currentplayer = #gameround.currentplayers.find(params[:id])
if #currentplayer.update(currentplayer_params)
redirect_to gameround_currentplayer_path
else
render 'edit'
end
end
# DELETE /currentplayers/1
# DELETE /currentplayers/1.json
def destroy
#gameround = Gameround.find(params[:gameround_id])
#currentplayer = #gameround.currentplayers.find(params[:id])
#currentplayer.destroy
respond_to do |format|
format.html { redirect_to '/play', notice: 'Currentplayer was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_currentplayer
#currentplayer = Currentplayer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def currentplayer_params
params.require(:currentplayer).permit(:log_id, :flares, :winner, :bases, :gameround_id, aliens:[])
end
end
config.routes
Rails.application.routes.draw do
resources :gamerounds do
resources :currentplayers
end
I figured it out. It was just because the submit button was outside of the main div. Sorry for the trouble!
You want to create your routes into config/routes.rb as follows:
resources :current_players

Rails 4: Correct syntax for nested resources - #user or :user?

I am using simple form for a nested resource.
resources :users do
resources :interests
end
I have the following call-backs in my InterestsController
class InterestsController < ApplicationController
before_action :set_interest, only: [:show, :create, :edit, :update, :destroy]
before_action :set_user, only: [:index, :show, :create, :edit, :update, :destroy]
private
# Use callbacks to share common setup or constraints between actions.
def set_interest
#interest = Interest.find(params[:id])
end
def set_user
#user = current_user
end
end
I have a simple form - this works......
<%= simple_form_for [:user, #interest] do |f| %>
<% if #interest.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#interest.errors.count, "error") %> prohibited this response from being saved:</h2>
<ul>
<% #interest.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-inputs">
<%= f.input :user_id %>
<%= f.input :discipline_id %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
But this does not.....
<%= simple_form_for [#user, #interest] do |f| %>
I can find no explanation for this - surely #user in the simple form is the correct was to declare the User?
Here is more information:
This is the error I get for the edit action:
No route matches {:action=>"show", :controller=>"interests", :format=>nil, :id=>nil, :user_id=>#<Interest id: 1, user_id: 1, discipline_id: 10, created_at: "2015-02-22 11:00:14", updated_at: "2015-02-22 11:00:14">} missing required keys: [:id]
Interestingly, the new action works. If I switch the simple form to
<%= simple_form_for [#user, #interest] do |f| %>
The edit action works but the new action does not. So I am guessing this has to be the way I have my InterestsController set up.
Here it is in full.
class InterestsController < ApplicationController
before_action :set_interest, only: [:show, :create, :edit, :update, :destroy]
before_action :set_user, only: [:index, :show, :create, :edit, :update, :destroy]
def index
#users_interests = #user.interests
end
def show
end
def new
#interest = current_user.interests.new
end
def edit
end
def create
#interest = User.find(params[:user_id]).interests.build(interest_params)
#interest.user = current_user
respond_to do |format|
if #interest.save
flash[:success] = "Interest created!"
format.html { redirect_to user_interests_path(current_user) }
format.json { render :show, status: :created, location: #interest }
else
flash[:failure] = "Interest was not added!"
format.html { render :new }
format.json { render json: #interest.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #interest.update(interest_params)
format.html { redirect_to user_interests_path(current_user) }
format.json { render :show, status: :ok, location: #interest }
else
format.html { render :edit }
format.json { render json: #interest.errors, status: :unprocessable_entity }
end
end
end
def destroy
#interest.destroy
respond_to do |format|
format.html { redirect_to interests_url, notice: 'Interest was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_interest
#interest = Interest.find(params[:id])
end
def set_user
#user = current_user
end
# Never trust parameters from the scary internet, only allow the white list through.
def interest_params
params.require(:interest).permit(:user_id, :discipline_id)
end
end
SOLVED:
easy fix - need to set #user = current_user in the new action:
def new
#interest = current_user.interests.new
#user =current_user
end

Resources