Ok so i am tring to list my genres in a f.select form and I am getting an error. I have look everywhere and i am just not understanding what i am doing differently. when I enter rails c and type g = Genre.all it lists all genres then g.map out puts => #<Enumerator: ...>
Error:
undefined method `map' for nil:NilClass
View Page:
<%= f.fields_for :genres do |g| %>
<div class="field">
<%= g.label :genre %>
<%= g.select :genres, #genres.map {|g| g.name} %>
</div>
<% end %>
Controller:
def create
#song = Song.new(params[:song])
#genres = Genre.all
if #song.save
redirect_to player_path
else
render :new
end
end
You need to assign #genres variable in new action too:
def new
#genres = Genre.all
end
Related
ActionView::Template::Error (undefined method `default_image' for nil:NilClass):
409:
410: <!-- Image -->
411: <div class="image">
412: <% if #product.default_image %>
413:
414: <%= image_tag #product.default_image.path, :weight => '262px',:height => '197px'%>
415: <% end %>
model:
# Return attachment for the default_image role
#
# #return [String]
def default_image
self.attachments.for("default_image")
end
# Set attachment for the default_image role
def default_image_file=(file)
self.attachments.build(file: file, role: 'default_image')
end
controller:
class ProductsController < ApplicationController
def index
#products = Shoppe::Product.root.ordered.includes(:product_categories, :variants)
#products = #products.group_by(&:product_category)
#product = Shoppe::Product.root.find_by_permalink(params[:permalink])
#order = Shoppe::Order.find(current_order.id)
end
end
#product = Shoppe::Product.root.find_by_permalink(params[:permalink]) returns nil. So when you try to call default_image on it then it breaks.
You could check it exists via
<% if #product && #product.default_image %>
<%= image_tag #product.default_image.path, :weight => '262px',:height => '197px'%>
<% end %>
Or if you wanted to raise an error if it doesn't exist you can use the bang method of the dynamic finder.
#product = Shoppe::Product.root.find_by_permalink!(params[:permalink])
Also that's the older syntax for dynamic finders. The new one is
#product = Shoppe::Product.root.find_by!(permalink: params[:permalink])
I'm using rails 4.0.8. I added a comment section to a model called 'Things', but I keep getting the same error "param is missing or the value is empty: thing" when I press the submit comment button. It says the error is in the Things#Controller. What am I doing wrong?
UPDATE: I removed the url path from the form, but a new error returns "Couldn't find Thing without an ID". The error is in Comments#Controller.
VIEW FOR THING/SHOW
<div id= "thing">
<h1>
<%= #thing.name %>
</h1>
<br>
<div id= "commentsection">
Comments
<div id= "comments">
<br>
<% #thing.comments.each do |c| %>
<%= c.username %>
<br>
<%= c.text %>
<% end %>
<%= form_for #comment, :url => thing_path do |f| %>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :comment %>
<%= f.text_field :text %>
<%= f.submit "Enter", class: "btn btn-small btn-primary" %>
<% end %>
</div>
</div>
</div>
THINGS CONTROLLER
class ThingsController < ApplicationController
def show
#thing = Thing.find(params[:id])
#thing.comments.build
#comment = Comment.new
end
def index
end
def new
#thing = Thing.new
#things = Thing.all
end
def create
#thing = Thing.new(thing_params)
if #thing.save
redirect_to #thing
else
render 'new'
end
end
private
def thing_params
params.require(:thing).permit(:name, :avatar)
end
end
COMMENTS CONTROLLER (I put asterisks around the line where the error is)
class CommentsController < ApplicationController
def show
#comment = Comment.find(params[:id])
end
def new
#comment = Comment.new
#comments = Comment.all
end
def create
****#thing = Thing.find(params[:thing_id])****
#comment = #thing.comments.create(comment_params)
redirect_to thing_path(#thing)
end
end
private
def comment_params
params.require(:comment).permit(:user, :text, :upvotes, :downvotes, :thing_id)
end
end
ROUTES
Website::Application.routes.draw do
get "comments/new"
get "comments/show"
get "things/new"
root 'home_page#home'
get "all/things/new" => 'things#new'
get "all/allthings"
resources :things
resources :good_comments
get "things/show"
get "things/results"
end
You are posting the #comment form to post '/things' path.
<%= form_for #comment, :url => thing_path do |f| %>
It should just be <%= form_for #comment do %> (Rails is smart enough to plug in the comments_path) or if you feel like being more explicit (even though it's not necessary)
<%= form_for #comment, url: :comments_path do %>
Another note though, if you want that Comment to be tied to that specific Thing then in your models it should be
Class Thing
has_many :comments
end
Class Comment
belongs_to :thing
end
Then make sure in your database comment has a thing_id foreign_key field and then your form for comment should actually look like
<%= form_for #thing, #comment do %>
<% end %>
I am trying to allow a user to enter a project into a database. One of the fields allows them to enter multiple technologies for that project.
Here is my project controller, new and create action.
def new
#project = Project.new
#all_technols = Technol.all
#project_technol = #project.projecttechnols.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #project }
end
end
def create
#project = Project.new(params[:project])
params[:technols][:id].each do |technol|
if !technol.empty?
#project.projecttechnols.build(:technol_id => technol)
end
end
end
Here is my new project view for the multi select technology dropdown.
<%= fields_for(#project_technol) do |ab| %>
<div class="tech">
<%= ab.label "All Tech" %><br/>
<%= collection_select(:technols, :id, #all_technols, :id, :tech, {}, {:multiple => true} ) %>
</div>
<% end %>
project.rb
class Project < ActiveRecord::Base
attr_accessible :tech
has_many :projecttechnols
has_many :technols, :through => :projecttechnols
end
technol.rb
class Technol < ActiveRecord::Base
attr_accessible :tech
has_many :projecttechnols
has_many :projects, :through => :projecttechnols
end
projecttechnol.rb
class Projecttechnol < ActiveRecord::Base
attr_accessible :project_id, :technol_id
belongs_to :technol
belongs_to :project
end
At the moment, I have a page where the user can enter a new technology. But I want to move this option to the create new project page, where they can either select existing technologies, or enter a new one, or do both, and they would save with that project.
When I try to save a new project however, I am getting this error.
Showing /home/james/Desktop/webapp/app/views/projects/new.html.erb where line #233 raised:
undefined method `model_name' for NilClass:Class
Extracted source (around line #233):
233: <%= fields_for(#project_technol) do |ab| %>
234:
235: <div class="tech">
236: <%= ab.label "All Tech" %><br/>
I am new to rails and still learning so please remember when answering. Thanks in advance.
EDIT
after changing
#project.projecttechnols.build(:technol_id => technol)
to
#project_technol = #project.projecttechnols.build(:technol_id => technol)
I now get this error:
NoMethodError in Projects#create
undefined method `map' for nil:NilClass
Extracted source (around line #240):
237: <div class="tech">
238: <%= ab.label "All Tech" %><br/>
239:
240: <%= collection_select(:technols, :id, #all_technols, :id, :tech, {}, {:multiple => true} ) %>
241: </div>
242: <% end %>
EDIT 2
#all_technols = Technol.all in the create action
I now get this error.
NoMethodError in Projects#show
Showing /home/james/Desktop/webapp/app/views/projects/show.html.erb where line #181 raised:
undefined method `technol' for #<Project:0xb36823c>
Extracted source (around line #181):
178: <h3>Related books</h3>
179:
180: <ul>
181: <% #project.technol.each do |technol| %>
182: <li><%= technol.tech %> <%= link_to "Details", technol_path(technol) %></li>
183: <% end %>
184: </ul>
Your create action is rendering the new view again. However, #project_technol is not defined within the create action. The fields_for method calls model_name method on the argument passed in (#project_technol), but since #project_technol = nil, it's throwing that error. To fix this, within your create action, change
#project.projecttechnols.build(:technol_id => technol)
to
#project_technol = #project.projecttechnols.build(:technol_id => technol)
I'm trying to create a partial template using <%= render "/shopping/coupons/cou" %> . Not really sure where went wrong. Thanks!
This is the error message.
undefined method `model_name' for NilClass:Class
Extracted source (around line #3):
1: <h4> Coupon </h4>
2:
3: <%= form_for(#coupon, :url => shopping_coupon_path(#coupon)) do |f| %>
4: <div class="field">
5: <%= f.label :code %>
6: <%= f.text_field :code %>
this is my coupons controller
class Shopping::CouponsController < Shopping::BaseController
def cou
form_info
end
def create
#coupon = Coupon.find_by_code(params[:coupon][:code])
if #coupon && #coupon.eligible?(session_order) && update_order_coupon_id(#coupon.id)
flash[:notice] = "Successfully added coupon code #{#coupon.code}."
redirect_to shopping_orders_url
else
form_info
flash[:notice] = "Sorry coupon code: #{params[:coupon][:code]} is not valid."
render :action => 'show'
end
end
private
def form_info
#coupon = Coupon.new
end
def update_order_coupon_id(id)
session_order.update_attributes( :coupon_id => id )
end
end
#coupon is nil when the view is being rendered.
The problem might be that <%= render "/shopping/coupons/cou" %> does not go through the cou action in the controller thus form_info method does not execute and #coupon does not get assigned a value.
You have to set #coupon in the action which renders the main view (the one which has the <%= render "/shopping/coupons/cou" %> in it).
I have a comment model that posts under a micropost like facebook. The problem is that I don't think I am writing the right code under the user show in the user controller and that is what is making this error pop up. Any suggestions? All help is much appreciated!
Error
NoMethodError in Users#show
Showing /Users/Brian/rails_projects/stateschool/app/views/microposts/_micropost.html.erb where line #70 raised:
undefined method `total_pages' for #<ActiveRecord::Relation:0x007fc8c2f83468>
Extracted source (around line #70):
67: <%= render :partial => "comments/form", :locals => { :micropost => micropost } %>
68: </div>
69: <div id='comments'>
70: <%= will_paginate micropost.comments, :class =>"pagination" %>
71: </div>
72: </div>
73:
This is my current user show page:
User Controller
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#school = School.find(params[:id])
#micropost = Micropost.new
#comment = Comment.new
#comment = #micropost.comments.build(params[:comment])
#comments = #micropost.comments.paginate(:page => params[:page], :per_page => 10)
#microposts = #user.microposts.paginate(:per_page => 10, :page => params[:page])
end
end
Thank you any suggestions are welcomed!
EDIT
<div id='comments'>
<%=render micropost.comments %>
<%= will_paginate #comments, :class =>"pagination" %>
</div>
try doing
<%= will_paginate #comments, :class =>"pagination" %>