private method `main_image' called for nil:NilClass? how to fix - ruby-on-rails

portfolios_controller.rb
class PortfoliosController < ApplicationController
def index
#portfolio_items = Portfolio.all
end
def new
#portfolio_item =Portfolio.new
end
def create
#portfolio_item =Portfolio.new(params.require(:portfolio).permit(:title, :subtitle, :body))
respond_to do |format|
if #portfolio_item.save
format.html { redirect_to portfolios_path, notice: 'Your Portfolio Item is now live.' }
format.json { render :show, status: :created, location: #blog }
else
format.html { render :new }
format.json { render json: #blog.errors, status: :unprocessable_entity }
end
end
end
def edit
#portfolio_item = Portfolio.find(params[:id])
end
def update
#portfolio_item = Portfolio.find(params[:id])
respond_to do |format|
if #portfolio_item.update(params.require(:portfolio).permit(:title, :subtitle, :body))
format.html { redirect_to portfolios_path, notice: 'The record successfully updated.' }
else
format.html { render :edit }
end
end
end
end
def show
#portfolio_item = Portfolio.find(params[:id])
end
show.html.erb
<%= image_tag #portfolio_item.main_image %>
<h1><%= #portfolio_item.title %></h1>
<em><%= #portfolio_item.subtitle %></em>
<p><%= #portfolio_item.body %></p>

Add show action
def show
#portfolio_item = Portfolio.find(params[:id])
end
In your portfolios_controller

private method `main_image' called for nil:NilClass?
You are ending the class PortfoliosController before show method, so it lost the scope. Put it inside the PortfoliosController
def update
#portfolio_item = Portfolio.find(params[:id])
respond_to do |format|
if #portfolio_item.update(params.require(:portfolio).permit(:title, :subtitle, :body))
format.html { redirect_to portfolios_path, notice: 'The record successfully updated.' }
else
format.html { render :edit }
end
end
end
def show
#portfolio_item = Portfolio.find(params[:id])
end
end #class end

Related

How to make not only one object of class ruby?

I need to create array of Events instead of one with same params except event.id . This is code :
def create
#event = Event.new(event_params)
#event.user_id = current_user.id
respond_to do |format|
if #event.save
format.html { redirect_to #event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: #event }
else
format.html { render :new }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
How should I do this?
UPD
Event params :
def event_params
params.require(:event).permit(:title, :description, :start_time, :end_time, :repeat)
end
def create
(1..10).each do |i|
params[:event][:user_id] = current_user.id
params[:event][:start_time] = params[:event][:start_time] + 24.hours if i > 1
params[:event][:end_time] = params[:event][:end_time] + 24.hours if i > 1
#event = Event.new(event_params)
#event.save
end
redirect_to events_path
end

undefined method using collect(&:foo).uniq

I am trying to show a collection of bookmark urls, catagorized by topic name for a current user. I am new to programming and I need help troubleshooting this error with #topics = #bookmarks.collect(&:topic).uniq causing the error:
undefined method `topic' for #<Bookmark:0xacfa42c>
Extracted source (around line #4):
def index
#bookmarks = current_user.bookmarks
#topics = #bookmarks.collect(&:topic).uniq
#liked_bookmarks = current_user.likes.collect(&:bookmark)
#liked_topics = #liked_bookmarks.collect(&:topic).uniq
end
app/controllers/user_bookmarks_controller.rb:4:in `index'
Here is my topic controller:
class TopicsController < ApplicationController
before_action :set_topic, only: [:show, :edit, :update, :destroy]
def index
#topics = Topic.all
end
def show
#bookmarks = #topic.bookmarks
end
def new
#topic = Topic.new
end
def edit
end
def create
#topic = Topic.new(topic_params)
respond_to do |format|
if #topic.save
format.html { redirect_to #topic, notice: 'Topic was successfully created.' }
format.json { render action: 'show', status: :created, location: #topic }
else
format.html { render action: 'new' }
format.json { render json: #topic.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #topic.update(topic_params)
format.html { redirect_to #topic, notice: 'Topic was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #topic.errors, status: :unprocessable_entity }
end
end
end
def destroy
#topic.destroy
respond_to do |format|
format.html { redirect_to topics_url }
format.json { head :no_content }
end
end
private
def set_topic
#topic = Topic.find(params[:id])
end
def topic_params
params.require(:topic).permit(:name)
end
end
Here is my bookmarks controller:
class BookmarksController < ApplicationController
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
def index
#bookmarks = Bookmark.all
end
def show
end
def new
#bookmark = Bookmark.new
end
def edit
end
def create
bookmark = Bookmark.where(url: params[:bookmark][:url]).first
#bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)
if #bookmark.save
#bookmark.users << current_user
Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{#bookmark.inspect}"
topic_names = params[:topic_names].split(' ')
topic_names.each do |topic_name|
name = topic_name.sub(/#/, '')
#bookmark.topics << Topic.find_or_create_by_name(name)
end
respond_to do |format|
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully created.' }
format.json { render action: 'show', status: :created, location: #bookmark }
end
else
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #bookmark.update(bookmark_params)
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
#bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url }
format.json { head :no_content }
end
end
private
def set_bookmark
#bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.require(:bookmark).permit(:url)
end
end
Here is my incoming (bookmarks and topics) controller:
class BookmarksController < ApplicationController
before_action :set_bookmark, only: [:show, :edit, :update, :destroy]
def index
#bookmarks = Bookmark.all
end
def show
end
def new
#bookmark = Bookmark.new
end
def edit
end
def create
bookmark = Bookmark.where(url: params[:bookmark][:url]).first
#bookmark = bookmark.present? ? bookmark : Bookmark.new(bookmark_params)
if #bookmark.save
#bookmark.users << current_user
Rails.logger.info ">>>>>>>>>>>>> Bookmark: #{#bookmark.inspect}"
topic_names = params[:topic_names].split(' ')
topic_names.each do |topic_name|
name = topic_name.sub(/#/, '')
#bookmark.topics << Topic.find_or_create_by_name(name)
end
respond_to do |format|
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully created.' }
format.json { render action: 'show', status: :created, location: #bookmark }
end
else
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #bookmark.update(bookmark_params)
format.html { redirect_to #bookmark, notice: 'Bookmark was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #bookmark.errors, status: :unprocessable_entity }
end
end
end
def destroy
#bookmark.destroy
respond_to do |format|
format.html { redirect_to bookmarks_url }
format.json { head :no_content }
end
end
private
def set_bookmark
#bookmark = Bookmark.find(params[:id])
end
def bookmark_params
params.require(:bookmark).permit(:url)
end
end
Thanks in advance for your help!
Based on the info you've provided, it looks like your Bookmark model probably has many topics. A given Bookmark will have a method called topics but not topic. You may want to try something like:
#topics = #bookmarks.topics.uniq

Missing Template and Controller Issues in Rails Project (rating)

I am stuck on a problem and i would love a hand! I got the polymorphic association from railscast #154
So far I have gotten the comment model working splendidly. However I want to create a rating model as well that I can then add js to make pretty.
Looked at every other tutorial/stackoverflows I could find. None of them show how to work with multiple polymorphic associations in a controller.
I tried to duplicate the code for the article_controller but I am not sure how to do that for both models. So far my attempts have been unsuccessful. The partial form has the proper name, it is _ratings.html.erb
My Error:
Missing partial ratings/ratings with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "/code/jimbo/bigdog/app/views"
* "/home/user/.rvm/gems/ruby-2.0.0-p247#lulla-core/gems/kaminari-0.14.1/app/views"
* "/home/user/.rvm/gems/ruby-2.0.0-p247#lulla-core/gems/devise-3.1.1/app/views"
My Models:
class Rating
include Mongoid::Document
include Mongoid::MultiParameterAttributes
field :value, type: Integer, default: "0"
belongs_to :rateable, polymorphic: true
end
My Controllers:
class RatingsController < ApplicationController
before_filter :load_rateable
def index
#ratings = #rateable.rating
end
def show
#rating = #rateable.ratings.find(params[:id])
respond_to do |format|
format.html # show.html.erb
end
end
def new
#rating = #rateable.rating.new
end
def create
#rating = #rateable.rating.new(params[:rating])
if #rating.save
redirect_to #rateable, notice: "Rating created."
else
render :new
end
end
private
def load_rateable
resource, id = request.path.split('/')[1, 2]
#rateable = resource.singularize.classify.constantize.find(id)
end
# def load_commentable
# klass = [Article, Photo, Event].detect { |c| params["#{c.name.underscore}_id"] }
# #rateable = klass.find(params["#{klass.name.underscore}_id"])
# end
end
Article Controller
class ArticlesController < ApplicationController
# GET /articles
# GET /articles.json
def index
#articles = Article.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #articles }
end
end
# GET /articles/1
# GET /articles/1.json
def show
#article = Article.find(params[:id])
#commentable = #article
#comments = #commentable.comments
#comment = Comment.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: #article }
end
end
# GET /articles/new
# GET /articles/new.json
def new
#article = Article.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #article }
end
end
# GET /articles/1/edit
def edit
#article = Article.find(params[:id])
end
# POST /articles
# POST /articles.json
def create
#article = Article.new(params[:article])
respond_to do |format|
if #article.save
format.html { redirect_to #article, notice: 'Article was successfully created.' }
format.json { render json: #article, status: :created, location: #article }
else
format.html { render action: "new" }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# PUT /articles/1
# PUT /articles/1.json
def update
#article = Article.find(params[:id])
respond_to do |format|
if #article.update_attributes(params[:article])
format.html { redirect_to #article, notice: 'Article was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #article.errors, status: :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.json
def destroy
#article = Article.find(params[:id])
#article.destroy
respond_to do |format|
format.html { redirect_to articles_url }
format.json { head :no_content }
end
end
end
My View:
show.html (article)
<%= render "comments/comments" %>
<%= render "comments/form" %>
<%= render "ratings/ratings" %>
<%= render "ratings/form" %>
Routes:
resources :articles do
resources :comments
resources :ratings
end

Rails has_many :through creating new and linking in controller

I have a website I am making that tracks a users companies through employments. I need to know what I am doing wrong because when I make a new user company the user doesn't know about it.
companies_controller.rb
class CompaniesController < ApplicationController
# GET /companies
# GET /companies.json
def index
#companies = current_user.companies
respond_to do |format|
format.html # index.html.erb
format.json { render json: #companies }
end
end
# GET /companies/1
# GET /companies/1.json
def show
#company = Company.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #company }
end
end
# GET /companies/new
# GET /companies/new.json
def new
#company = Company.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #company }
end
end
# GET /companies/1/edit
def edit
#company = Company.find(params[:id])
end
# POST /companies
# POST /companies.json
def create
#company = Company.new(params[:company])
current_user.employments.create!(company_id: #company.id)
respond_to do |format|
if #company.save
format.html { redirect_to #company, notice: 'Company was successfully created.' }
format.json { render json: #company, status: :created, location: #company }
else
format.html { render action: "new" }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
end
end
# PUT /companies/1
# PUT /companies/1.json
def update
#company = Company.find(params[:id])
respond_to do |format|
if #company.update_attributes(params[:company])
format.html { redirect_to #company, notice: 'Company was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #company.errors, status: :unprocessable_entity }
end
end
end
# DELETE /companies/1
# DELETE /companies/1.json
def destroy
#company = Company.find(params[:id])
#company.destroy
respond_to do |format|
format.html { redirect_to companies_url }
format.json { head :no_content }
end
end
end
The problem is within your create action, specifically the line
current_user.employments.create!(company_id: #company.id)
this is executed before the company record is saved so it doesn't have an id (== nil). Just move that line after
if #company.save
and it should attach it to the current_user via the :through relationship.

Why's is Rails giving me an error about the 'create' action not being in my controller?

When I press new on my Jobs form in seeing an error that it could not find 'create' in my JobsController.
Unknown action
The action 'create' could not be found for JobsController
Here' my controller:
class JobsController < ApplicationController
private
def load_clients
#clients = collection_select :client, :client_id
end
def index
#job = Job.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #job }
end
end
def create
#job = Job.new(params[:job])
respond_to do |format|
if #job.save
format.html { redirect_to #job, notice: 'Job was successfully created.' }
format.json { render json: #job, status: :created, location: #job }
else
format.html { render action: "new" }
format.json { render json: #job.errors, status: :unprocessable_entity }
end
end
end
def show
#job = Job.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #job }
end
end
end
As you can see. It' clearly there. Why is Rails not seeing it?
It is because you assign the create method as private.
Try redefine your controller this way.
Here' my controller:
class JobsController < ApplicationController
def index
...
end
def create
...
end
def show
...
end
private
def load_clients
#clients = collection_select :client, :client_id
end
end

Resources