Trying to make sub categories of categories in nested resources. The issue that I have is that it doesn't seem to work. All i get are undefined methodsub_category_path' for #<#:0x007f8fcde33550>
Did you mean? new_category_pathswell asfirst value of a form cannot be nil`.
My controller methods are as follows:
SubCategory Update:
#category = #subcategory.category.id
if #subcategory.update(subcategory_params)
flash[:success] = "Subcat name updated"
redirect_to category_sub_category_path(#subcategory)
else
render 'edit'
end
SubCategory new:
#category = Category.find(params[:category_id]) # TODO:subcategory.category.id?
#category_sub = #category.sub_categories.new
##subcategory = #category.sub_categories.new
SubCategory create:
#subcategory = SubCategory.new(subcategory_params)
if #subcategory.save
flash[:success] = "subcategory created"
redirect_to category_sub_category_path
else
flash[:error] = "subcategory failed"
render 'new'
end
And my private methods:
private
def set_sub_category
#subcategory = SubCategory.find(params[:id])
end
def subcategory_params
params.require(:sub_category).require(:name, :category_id)
end
end
I don't have a SubCategories index, the viewing of the SubCategory is based off clicking a link in Categories index.html.erb which can be seen here:
<%#categories.each do |c|%>
<ul class="listing">
<div class="row">
<div class="well col-md-4 col-md-offset-4">
<li class="article-title">
<strong><%= link_to "#{c.name}", category_path(c)%></strong>
<% c.sub_categories.each do |s|%>
<div>
<%=link_to s.name, category_sub_category_path(c,s)%>
</div>
<%end%>
</li>
<li><small>
<!--Pluralise here-->
</small></li>
</div>
</div>
</ul>
<%end%>
And then in my SubCategory show.html.erb:
<span class ="badge"><%= link_to "Edit sub category name",
edit_category_sub_category_path([#category, #subcategory])%></span>
And in my SubCategory edit.html.erb:
<%=form_for([#category, #subcategory], :html => {class: "form-horizontal", role: "form"})do |f|%>
<div class="form-group">
<div class="control-label col-sm-2">
<%= f.label :subcategory%>
</div>
<div class="col-sm-8">
<%= f.text_field :name, class: "form-control", placeholder: "Update Sub Category", autofocus: true%>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<%=f.submit class: "btn btn-primary btn-lg"%>
</div>
</div>
<%end%>
Any help would be appreciated
EDIT:
Errors
am now getting this error
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"81lwz0NufTqdOni+4wPRO4Vnmeis9f32+sqSNsxTXZY7ufyR8hTvF86KQwdMjcOP2DQJibK66croW48Uhos9+A==", "sub_category"=>{"name"=>"Gold", "category_id"=>"1"}, "commit"=>"Update Sub category", "category_id"=>"1", "id"=>"1"}
because wrong number of arguments (given 2 expects 1)
def
subcategory_params params.require(:sub_category).require(:name, :category_id)
end
FIXED: Just took out the second .require, no idea why I put that there
As you have mentioned in the comments the edit action must be like
def edit
#subcategory = SubCategory.find(params[:id])
end
So the value #category is nil this has caused a error first value of a form cannot be nil.
Make the edit action as
def edit
#subcategory = SubCategory.find(params[:id])
#category = #subcategory.category
end
Assuming that you have relation between category and sub category.
Related
Hello guys I searched everywhere on how to output form errors but no luck. I tried almost all the code I found on the internet but still didn't work.
Here are some of my files:
view/books/new.html.erb
<div class="container">
<h2>Create a new book</h2>
<%= form_with model: #book, url: create_new_book_path do |f| %>
<% if #book.errors.any? %>
<ul>
<% #book.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div class="form-group row">
<%= label_tag(:title, "Title:", class: "col-sm-2 col-form-label") %>
<div class="col-sm-10">
<%= f.text_field :title, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= label_tag(:price, "Price:", class: "col-sm-2 col-form-label") %>
<div class="col-sm-10">
<%= f.text_field :price, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= label_tag(:subject_id, "Subject:", class: "col-sm-2 col-form-label") %>
<div class="col-sm-10">
<%= f.collection_select :subject_id, #subjects, :id, :name, class: "form-control" %>
</div>
</div>
<div class="form-group row">
<%= label_tag(:description, "Book description:", class: "col-sm-2 col-form-label") %>
<div class="col-sm-10">
<%= f.text_area :description, class: "form-control" %>
</div>
</div>
<%= submit_tag "Create book", class: "btn btn-success" %>
<%= link_to "Cancel", books_path, class: "btn btn-danger" %>
<% end %>
<br/>
</div>
books_controller.rb
class BooksController < ApplicationController
def index
#books = Book.all
end
def show
#book = Book.find(params[:id])
end
def new
#book = Book.new
#subjects = Subject.all
end
def create
#book = Book.new(book_params)
end
def book_params
params.require(:book).permit(:title, :price, :subject_id, :description)
end
def edit
#book = Book.find(params[:id])
#subjects = Subject.all
end
def update
#book = Book.find(params[:id])
if #book.update_attributes(book_params)
redirect_to action: "index"
else
#subjects = Subject.all
render action: "edit"
end
end
def destroy
Book.find(params[:id]).destroy
redirect_to action: "index"
end
end
routes.rb
get 'books/new', to: 'books#new', as: 'new_book'
post 'books/create', to: 'books#create', as: 'create_new_book'
view/layouts/application.html.erb
<main role="main">
<%= yield %>
</main>
I don't really know what I'm missing to get the errors, grateful for your answers!!
try putting them in a flash like this and render it in your view
def create
#book = Book.new
if #book.update(book_params)
flash[:notice] = 'success'
redirect_to action: 'index'
else
flash[:alert] = #book.errors.full_messages.join(', ')
redirect_to action: 'index'
end
end
and in your view render those flashes like
<% if flash[:notice]%>
<span class='notice'><%= flash[:notice] %></span>
<% end %>
<% if flash[:alert]%>
<span class='alert'><%= flash[:alert] %></span>
<% end %>
Link to image of what I'm talking about
My problem is that there is an extra template being rendered at the bottom of my list without an item id. The icons delete the item they are next to when clicked. The bottom icon is showing up even though it has no item related to it.
Here is my the show html.erb
<div class="row">
<div class="col-md-8">
<div class="media">
<br />
<div class="media-body">
<h3><%= #user.email %></h3>
<br />
<%= render partial: 'items/form', locals: {user: #user} %>
<div class="media">
<div class="media-body">
<h4 class="media-heading">
<ul>
<%= render #items %>
</ul>
</h4>
</div>
</div>
</div>
</div>
</div>
Here is my _item template
<li><%= item.name %><%= link_to "", [current_user, item], remote: true, method: :delete, class: 'glyphicon glyphicon-ok' %></li>
Here is my items controller
class ItemsController < ApplicationController
def create
#item = Item.new(item_params)
#item.user = current_user
if #item.save
flash[:notice] = "Item was saved."
redirect_to user_path(current_user.id)
else
flash.now[:alert] = "There was an error saving the item. Try again."
redirect_to user_path(current_user.id)
end
end
def destroy
#item = Item.find(params[:id])
if #item.destroy
flash[:notice] = "Item deleted"
redirect_to user_path(current_user.id)
else
flash.now[:alert] = "Error deleting the item."
redirect_to user_path(current_user.id)
end
end
private
def item_params
params.require(:item).permit(:name)
end
end
Here is my users controller
class UsersController < ApplicationController
def show
#user = current_user
#item = Item.new
#items = #user.items
end
end
The items belong to the users and have nested routes under user. The last checkmark (the one I want to get rid of) when hovered over shows the link .../users/6/items, while the others that next to items show .../users/6/items/40
Here is my _form.html.erb for items:
<%= form_for [user, user.items.new] do |f| %>
<%= f.text_field :name, class: 'form-control', placeholder: "Enter your to-do item", value: nil, required: true %>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
Remove
#item = Item.new
from show action in users controller.
class UsersController < ApplicationController
def show
#user = current_user
#items = #user.items
end
end
I am working on a rails assignment that needs me to create a checkbox that destroys items on a to-do list. I am currently getting a undefined local variable or method `item,' but I don't think thats the overall problem with my items#destroy action. Should I be making item to be #item instead?
Items Controller:
class ItemsController < ApplicationController
def new
#user = User.find(params[:user_id])
#item = Item.new
end
def create
#user = User.find(params[:user_id])
#item = Item.new(params.require(:item).permit(:name))
#item.user_id = #user.id
if #item.save
flash[:notice] = "Item was saved."
redirect_to [#user, #item]
else
flash[:error] = "There was an error saving the item. Please try again."
render :new
end
end
def show
#user = User.find(params[:user_id])
#item = Item.find(params[:id])
end
def destroy
#user = User.find(params[:user_id])
#item = Item.find(params[:id])
if #item.destroy
flash[:notice] = "Item was removed."
else
flash[:error] = "Item couldn't be deleted. Try again."
end
respond_to do |format|
format.html
format.js
end
end
Destroy.js.erb
$('#item-' +<%= #item.id %>).hide();
Users Show:
<br>
<div class="container">
<div class="row">
<div class="col-md-11">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-md-12 lead">User profile<hr></div>
</div>
<div class="row">
<div class="col-md-4 text-center">
<img class="img-circle avatar avatar-original" style="-webkit-user-select:none;
display:block; margin:auto;" src="http://robohash.org/sitsequiquia.png?size=120x120">
</div>
<div class="col-md-8">
<div class="row">
<div class="col-md-12">
<h1 class="only-bottom-margin"><%= #user.email%></h1>
</div>
</div>
<div class="row">
<div class="col-md-6">
<span class="text-muted">Name:</span> <%= #user.name%><br>
<span class="text-muted">Id:</span> <%= #user.id %><br>
</div>
<div class="col-md-10">
<h3>User's To-Do List</h3>
<%= link_to "", item, method: :delete, class: 'glyphicon glyphicon-ok', remote: true %>
<%= render partial: 'items/item'%>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
By the time your view is called (i.e. destroy.js.erb), #item will be destroyed. Just add a variable that caches the item id:
def destroy
#user = User.find(params[:user_id])
#item = Item.find(params[:id])
#item_id = #item.id
if #item.destroy
flash[:notice] = "Item was removed."
else
flash[:error] = "Item couldn't be deleted. Try again."
end
respond_to do |format|
format.html
format.js
end
and then in your destroy.js.erb:
$('#item-' +<%= #item_id %>).hide();
I believe the error is being introduced in your view with the line:
<%= link_to "", item, method: :delete, class: 'glyphicon glyphicon-ok', remote: true %>
I don't see a setter for 'item' anywhere, which would prevent the link_to method from inferring its type and route.
What appears to be missing is a loop through the user's items, something along the lines of...
<h3>User's To-Do List</h3>
<% #user.items.each do |item| %>
<%= link_to "", item, method: :delete, class: 'glyphicon glyphicon-ok', remote: true %>
<%= render partial: 'items/item'%>
<% end %>
Note that this solution makes an assumption about the relationship between User and Item, specifically a has_many :items in the User.rb model.
I'm getting an undefined method 'each' for nil class in my create method for a join table that I have.
I've got one join table for Emotions_pins and one for casues_pins the emotions pins table works fine but I'm getting the error on causes. Here's the code
_form.html.erb for Pin
<%= form_for(#pin) do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<% if #pin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#pin.errors.count, "error") %> prohibited this checkin from being saved:</h2>
<ul>
<% #pin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<h3>Hi! Thank you for choosing to check-in with your teacher! This is a great way to get help, share your feelings and concerns, and make your school a safer place to learn. </h3>
<div class="form-group">
<%= label_tag(:classroom, "Select your classroom:") %>
<%= select_tag "pin[code]", options_from_collection_for_select(Classroom.all, "code", "code", ) %>
</div>
<h4>Where?</h4>
<% #causes.each do |cause| %>
<div class="checkbox">
<ul>
<li>
<%= check_box_tag "reflection[cause_ids][]", cause.id %>
<%= label_tag(cause.name) %>
</li>
<ul>
</div>
<% end %>
<div class="form-group">
<%= image_tag 'feelings.png', class: "image" %>
<h4>How are you feeling?</h4>
</div>
<% #emotions.each do |emotion| %>
<div class="checkbox">
<%= check_box_tag "pin[emotion_ids][]", emotion.id %>
<%= label_tag(emotion.name) %>
</div>
<% end %>
<div class="form-group">
<h4> You can <strong>free write </strong> </h4>
<p> I want my teacher to know _____________________________________.</p>
<%= f.text_area :question, class: "form-control" %>
</div>
<div class="form-group">
<h4> You can write about your own actions or thoughts here.</h4>
<p>Something I did was ________________________________.</p>
<%= f.text_area :question1, class: "form-control" %>
</div>
<div class="form-group">
<h4>You can write about the actions of another person here..</h4>
<p>Something __(name)_____did was___________________________________.</p>
<%= f.text_area :question2, class: "form-control" %>
</div>
<div class="form-group">
<h4>Do you have a question for your teacher?</h4>
<p>I want to ask my teacher ______________________________________.</p>
<%= f.text_area :question3, class: "form-control" %>
</div>
<div class="form-group">
<h4>Are you thinking about <strong>doing something else</strong>? You can write about it here.</h4>
<p>Something else I might do is ______________________________________.</p>
<%= f.text_area :question4, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "submit", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
EDIT [ ADDED pin_controller.rb]
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
respond_to :html s
def home
#pins = Pin.all
respond_with(#pins)
authorize #pins
end
def show
respond_with(#pin)
end
def new
#pin = Pin.new
#emotions = Emotion.all
#causes = Cause.all
#school = School.find(params[:school])
respond_with(#pin)
authorize #pin
end
def edit
end
def create
code = params[:pin][:code]
#classroom = Classroom.where('code LIKE ?', code).first
unless #classroom
flash[:error] = "Classroom code incorrect"
#emotions = Emotion.all
#causes = Cause.all
render :new
else
params[:pin][:classroom_id] = #classroom.id
#pin = Pin.new(pin_params)
#pin.save
params[:pin][:cause_ids].each do |cause_id|
#cause = Cause.find(cause_id)
#pin.causes << #cause
end
params[:pin][:emotion_ids].each do |emotion_id|
#emotion = Emotion.find(emotion_id)
#pin.emotions << #emotion
end
if #pin.save
redirect_to signout_path and return
end
respond_with(#pin)
authorize #pin
end
end
def update
#pin.update(pin_params)
respond_with(#pin)
authorize #pin
end
def destroy
#pin.destroy
respond_with(#pin)
authorize #pin
end
private
def set_pin
#pin = Pin.find(params[:id])
authorize #pin
end
def pin_params
params.require(:pin).permit(:user_id, :question, :question1, :question2,
:question3, :question4, :question5, :classroom_id, :sad,
:happy, :mad, :brave, :embarrassed, :sorry, :frustrated,
:silly, :left_out, :excited, :hurt, :jealous, :confused,
:proud, :other)
end
end
Here's the exact error I'm getting
I've so far been unable to figure out the problem. What am I missing?
Stupid error but didn't catch it until I posted the form code... Thanks for the help!!
"reflection[cause_ids][]"
should be
"pin[cause_ids][]"
Thanks again for the help:)
<h4>Where?</h4>
<% #causes.each do |cause| %>
<div class="checkbox">
<ul>
<li>
<%= check_box_tag "reflection[cause_ids][]", cause.id %>
<%= label_tag(cause.name) %>
</li>
<ul>
</div>
<% end %>
My modal is working ( means when i click button it appears) but the form is not appearing, i have this modal in the index action so i have added <% #entries.each do |entry| %> and without it i am having undefined methodmodel_name' for NilClass:Class, any suggestions ?
<%= link_to "Assign", "#saturday-one", :class => "btn btn-primary", "data-toggle" => "modal" %>
<div class="modal hide fade" id="saturday-one">
<div class="modal-header">
<h3 id="myModalLabel">Time Table</h3>
</div>
<div class="modal-body">
<% #entries.each do |entry| %>
<%= simple_form_for(entry) do |f| %>
<%= f.association :subject, as: :select, label: 'Select Subject' %>
<%= f.association :employee, as: :select, label: 'Assign Employee' %>
<%= f.submit %>
<% end %>
<% end %>
</div>
<div class="modal-footer">
<button aria-hidden="true" class="btn btn-info" data-dismiss="modal">Done</button>
</div>
</div>
time_table_entries_controller.rb
class TimeTableEntriesController < ApplicationController
def index
#entries = TimeTableEntry.all
end
def show
#entry = TimeTableEntry.find(params[:id])
end
def new
#entry = TimeTableEntry.new
end
def create
#entry = TimeTableEntry.new(params[:time_table])
if #entry.save
redirect_to time_table_entries_url , notice: 'Time table Entries saved'
else
render :new
end
end
def edit
#entry = TimeTableEntry.find(params[:id])
end
def update
#entry = TimeTableEntry.find(params[:id])
if #entry.update_attributes(params[:time_table])
redirect_to time_table_entries_url, notice: 'TimeTableEntry Updated.'
else
render :edit
end
end
end