I have a set up where Goals have Steps and Steps have Tasks. In my view I am showing a goal, if that goal has steps then I can pull those and show them as well. I am having a hard time showing the tasks for each individual step now. I can get the task associated with the proper step so part of this is working, but I cannot access any of the data, name and description specifically for the task.
I am just unsure how to create this in the controller. I have cruised around the guide trying to find a solution and tried things like
#goal.steps.tasks
#step.tasks
I just cannot find anything that clicks for creating the variable in the controller so I can access it.
goals_controller.rb show method
def show
#goal = Goal.find(params[:id])
#steps = #goal.steps(page: params[:page])
# #tasks = ?
end
goal.rb
class Goal < ActiveRecord::Base
has_many :steps, dependent: :destroy
has_many :tasks, through: :steps
end
step.rb
class Step < ActiveRecord::Base
belongs_to :goals
has_many :tasks, dependent: :destroy
default_scope -> { order(created_at: :desc) }
end
task.rb
class Task < ActiveRecord::Base
belongs_to :steps
end
views
goals/show view
<p>
<% if #goal.steps.any? %>
<!-- Projects Row -->
<div class="row">
<%= render #steps %>
</div>
<!-- /.row -->
<% else %>
No steps for this goal.
<% end %>
</p>
the partial rendered for the steps
<div id="steps-<%= step.id %>" class="col-md-3 portfolio-item not completed" style="background-color:#e26a5c;border-radius:10px;border:1px solid #6cbdc4;margin:0 5px 5px 0;">
<h3>
<p>In order to achieve this goal I need to <strong><%= step.name %></strong> in <strong><%= time_ago_in_words(step.deadline) %></strong></p>
</h3>
<img src="/img/icon-edit.png">
<% if step.tasks.any? %>
<%= render "tasks/task" %>
<% else %>
<%end %>
</div>
<% else %>
<div id="steps-<%= step.id %>" class="col-md-3 portfolio-item completed" style="background-color:#6cbdc4;border-radius:10px;border:1px solid #e26a5c;margin:0 5px 5px 0;">
<h3>
<p>In an effort to achieve this goal I needed to <strong><%= step.name %></strong> in <strong><%= time_ago_in_words(step.deadline) %></strong></p>
</h3>
<img src="/img/icon-edit.png">
</div>
Tasks/task is the partial I am trying to pull in, I can link to it on the correct step and everything, but it says undefined method when I put task.name or task.description in.
Try this in your goals_controller.rb:
def show
#goal = Goal.find(params[:id])
#steps = #goal.steps(page: params[:page]).includes(:tasks)
end
Then in your view you could have something like this:
<ul>
<% #steps.each do |step| %>
<li>
<%= step %>
<p>Tasks:</p>
<ul>
<% step.tasks.each do |task| %>
<li><%= task %></li>
<% end %>
</ul>
</li>
<% end %>
</ul>
For more info on includes: http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
Related
Making a basic phonebook app and made some changes while figuring out the has_many and belongs_to relationships. I must have broke something because I have no idea why I'm getting this error. When I access my root, I get the following-->
ActionController::UrlGenerationError in ContactsController#index
No route matches {:action=>"show", :controller=>"contacts"} missing required keys: [:id]
The error shows mistakes in lines:
app/views/contacts/index.html.erb:10:in `block in _app_views_contacts_index_html_erb___2771775118522806317_70170309989460'
app/views/contacts/index.html.erb:7:in `_app_views_contacts_index_html_erb___2771775118522806317_70170309989460'
This is my contacts/index.html.erb
<p id="notice"><%= notice %></p>
<% if user_signed_in? %>
<h1>Listing Contacts</h1>
<% #contacts = current_user.contacts %>
<% #contacts.each do |contact| %>
<div class="link row clearfix">
<h2>
<%= link_to contact.name, contact_path %>
</h2>
</div>
<% end %>
<% end %>
<%= link_to "New Contact", new_contact_path %>
<% else %>
<h5> Welcome. Make an account or sign in above! </h5>
<% end %>
This is my config/routes
Rails.application.routes.draw do
resources :controllers
devise_for :users
resources :contacts so
resources :numbers
end
end
end
This is my contacts/show.html.erb
<div class="page-header">
<h1><%= #contact.name %><br> </h1>
</div>
<p>
<strong>Name:</strong>
<%= #contact.name %>
</p>
<p>
<strong>Email:</strong>
<%= #contact.email %>
</p>
<br>
<%= link_to 'Edit', edit_contact_path(#contact) %> |
<%= link_to 'Back', contacts_path %>
The output of my rake routes:
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
As you can see, I have a route for contacts#show so that's not the mistake. I'm unclear as to what it could be. Any ideas?
Seems like you're missing :id from the contact_path.
In contacts/index.html.erb, change this:
<%= link_to contact.name, contact_path %>
to this:
<%= link_to contact.name, contact_path(contact.id) %>
Ok so Ive looked around at many answers posted on stackoverflow and many other sites because I know that this is a commonly asked question but I just cant seem to get it and would really appreciate it if someone could point me in the right direction. What Im trying to achieve here is really simple, Im trying to render a new form from another model, Post into my homepage, which is another controller that I created. So basically in my homepage it shows a list of all the recent Post that were created by users. Furthermore I wanted to user to be able to directly add a new Post from the homepage. Right now I have it displaying however clicking on the submit button does absolutely nothing.
Here is the form for POST
<div class="panel panel-primary">
<div class="container">
<div class="panel-heading">
<%= image_tag #post.user.avatar.url(:post_pic), class:"img-thumbnail" %>
<%= #post.user.name %>
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.file_field :avatar %>
</div>
</div>
</div>
<div class="panel-footer">
<div class="actions">
<%= f.submit %>
</div>
</div>
<% end %>
</div>
Here is my homepage controller
class HomeController < ApplicationController
def index
if user_signed_in?
#posts = Post.all
#post = current_user.posts.build
else
redirect_to new_user_registration_path
end
end
def create
#post = current_user.posts.build(post_params)
#post.save
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :content, :avatar)
end
end
Lastly my homepage index
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-5">
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<%= render 'posts/form' %>
<% #posts.each do |post|%>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<%= image_tag post.user.avatar.url(:post_pic), class:"img-thumbnail" %>
<%= post.user.name %>
</h3>
</div>
<div class="panel-body">
<%= post.content %></br></br>
<% if post.avatar.file? %>
<%= image_tag post.avatar.url(:test), class:"img-thumbnail" %></br>
<% end %>
</div>
</div>
<% end %>
</div>
</div>
Routes
Rails.application.routes.draw do
resources :posts
get 'users/index'
get 'users/show'
root 'home#index'
devise_for :users
So, you're doing like saving post data using another Controller(Home) instead of Post Controller, right?
Your partial form for post uses:
<%= form_for(#post) do |f| %>
// #post generated url (/posts)
// will look for create method in POST Controller
I suggest to put your saving codes in proper controller(posts), so you will prevent code duplication/redundancy.
TIP:
If you are worrying about redirection from Home view or from Posts view,
you can use params[:controller] for checking where the request came from, by just using if statement.
Finally, you can redirect it to proper page after saving.
Newbie on RoR. I want to search division, category and brand (by name, not by id). I have a problem to figure out a search function. Hopefully I can get out from my problem. What should I do to convert (division_id) --> (division_name), etc?
**#Model**
item.rb :
belongs_to :division
belongs_to :category
belongs_to :brand
def self.search(params)
items = Item.all
items = items.where("division_id BETWEEN #{params[:division_from].to_i} AND #{params[:division_to].to_i}") if params[:division_from].present?
items = items.where("category_id BETWEEN #{params[:category_from].to_i} AND #{params[:category_to].to_i}") if params[:category_from].present?
items = items.where("brand_id BETWEEN #{params[:brand_from].to_i} AND #{params[:brand_to].to_i}") if params[:brand_from].present?
end
category.rb :
has_many = items
division.rb :
has_many = items
brand.rb :
has_many = items
**#View**
items- index.html.erb
<fieldset><legend>Filter</legend></fieldset>
<div class="panel-body">
<div class="row">
<%= form_tag search_items_path, class: "form-horizontal bucket-form", remote: true do %>
<div class="col-md-6">
<div class="form-group">
<div class="text-field">
<div class="col-md-10">
<td>Division</td>
<div class="input-group input-large">
<%= text_field_tag 'division_from','', class: "form-control dpd1" %>
<span class="input-group-addon">To</span>
<%= text_field_tag 'division_to','', class: "form-control dpd1" %>
</div>
<td>Category</td>
<div class="input-group input-large">
<%= text_field_tag 'category_from','', class: "form-control dpd1" %>
<span class="input-group-addon">To</span>
<%= text_field_tag 'category_to','', class: "form-control dpd1" %>
</div>
<td>Brand</td>
<div class="input-group input-large">
<%= text_field_tag 'brand_from','', class: "form-control dpd1" %>
<span class="input-group-addon">To</span>
<%= text_field_tag 'brand_to','', class: "form-control dpd1" %>
</div>
<div class="btn-group">
<%= submit_tag 'Search', class: "btn btn-primary" %>
</div>
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
**#Controller**
items_controller.rb :
def search
#items = Item.search(params)
respond_to do |format|
format.js
end
end
If I execute my code, I have search by id (not by name).. Thanks before
Well.. first, if you want to search only one division-category-brand, change the view for every item only one text input (but its better to use select/multiselect).
Using text input:
only one text input per class
e.g. <%= text_field_tag 'brand','', class: "form-control dpd1" %>
in the item.rb change the search method to this
def self.search(params)
items = Item.all
items = items.where(division_id:Division.find_by_name(params[:division])) if params[:division].present?
items = items.where(category_id:Category.find_by_name(params[:category])) if params[:category].present? items = items.where(brand_id:Brand.find_by_name(params[:brand])) if params[:division].present?
end
It will not work when you're not filling the correct name in the view, so i suggest to use select/multiselect.
I hope it will work.
I am trying to show the 'status' & 'project' from an '#user' on that users profile page, I believe I have the correct relationships etc but the error I am getting is that I cannot call render multiple times in one action.
class ProfilesController < ApplicationController
def show
#user = User.find_by_profile_name(params[:id])
if #user
#statuses = #user.statuses.all
render actions: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
#user = User.find_by_profile_name(params[:id])
if #user
#projects = #user.projects.all
render actions: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
end
What is the best way to express the above, to render both items on the users profile page without calling render twice?
This is my view:
<div class="container">
<div class="page-header">
<h1><%= "Hi " + #user.first_name + "!" %>
</div>
<div class="col-md-6 col-md-offset-3">
<%= link_to "Create Project", new_project_path, class: "btn btn-success btn-block" %>
</div>
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end %>
<% if #projects %>
<% #projects.each do |project| %>
<div class="well">
<%= project.title %>
<hr />
<%= link_to time_ago_in_words(project.created_at), projects_path(project) %> ago
</div>
<% end %>
<% end %>
</div>
Thanks for your time!
Try as below.
class ProfilesController < ApplicationController
def show
#user = User.find_by_profile_name(params[:id])
if #user
#statuses = #user.statuses.all
#projects = #user.projects.all
render action: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
end
I've been looking all over the web for an answer to this for the past three hours so I'm going to post a question. I'm trying to create some basic forum software but I am having trouble with my relationships. This is the error I get
undefined method `forums' for # <ActiveRecord::Relation::ActiveRecord_Relation_Category:0x3706cb0>
my categories/index file
<% for category in #category %>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"><%= link_to category.name, category_path(category.id) %></h3>
<h4><%= category.description %> </h4>
</div>
<% for forum in #category.forums %>
<div class="panel-body">
<h4><span class="glyphicon glyphicon-tree-conifer"></span><%= link_to forum.name, forum_path(forum.id) %> </h4>
</div>
<hr />
<% end %>
</div>
<% end %>
Categories Controller
class CategoriesController < ApplicationController
def index
#category = Category.all
end
def show
#category = Category.find(params[:id])
end
end
forums controller
class ForumsController < ApplicationController
def show
#forum = Forum.find(params[:id])
end
end
category model
class Category < ActiveRecord::Base
has_many :forums, dependent: :destroy
end
forum model
class Forum < ActiveRecord::Base
belongs_to :category
has_many :threads, dependent: :destroy
end
and my routes
ForumName::Application.routes.draw do
root 'static_pages#home'
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :categories, :path => "forum"
resources :forums
resources :topics
resources :posts
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
end
Hopefully I've provided enough info if anything else is required I'll update the post. Usually I can work these things out using google but this really has me stumped.Thanks in advance.
Issue in action CategoriesController#index
Should be changed to
#categories = Category.all
because Category.all expects to get several categories, not one
Also you should change categories/index file
<% for category in #categories %>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"><%= link_to category.name, category_path(category.id) %></h3>
<h4><%= category.description %> </h4>
</div>
<% for forum in category.forums %>
<div class="panel-body">
<h4><span class="glyphicon glyphicon-tree-conifer"></span><%= link_to forum.name, forum_path(forum.id) %> </h4>
</div>
<hr />
<% end %>
</div>
<% end %>