I encountered this error today, I search google for solution but I cannot find the correct answer for this. The error:
NoMethodError at /master/hotels/import
undefined method `id' for nil:NilClass
This is the templete file:
row
.col-xs-12
%p.pull-left
%button.btn.btn-white.btn-sm
一括削除
%p.pull-right
= link_to "新規作成", url_for(action: :new), class: "btn btn-white btn-sm"
= link_to "CSV Export", url_for(action: :index, format: 'csv'), class: "btn btn-white btn-sm"
= link_to "CSV Upload", url_for(action: :import), class: "btn btn-white btn-sm"
-#= form_tag url_for(action: :import), class: 'pull-right' do |f|
-#= file_field_tag :csv, as: :file
-#= submit_tag "CSV Upload", input_html: {class: "btn btn-white btn-sm"}
.col-xs-12
= render 'cruds/grid'
and here is my controller file:
class CrudsController < ApplicationController
before_action :load_crud
before_action :set_crud, only: [:show, :edit, :update, :destroy]
def load_crud
end
# GET /cruds
# GET /cruds.json
def index
#items_grid = initialize_grid(#model)
#csv = CSV.generate() do |csv|
csv << #model.column_names
#model.all.each do |item|
csv << item.attributes.values_at(*#model.column_names).map{|i| i.to_s.encode("cp932", "UTF-8")}
end
end
respond_to do |format|
format.html { render template: 'cruds/index'}
format.csv { send_data #csv }
#format.xls # {send_data #product.to_csv (col_sep: "|t")}
end
# render template: 'cruds/index'
end
# GET /cruds/1
# GET /cruds/1.json
def show
render template: 'cruds/show'
end
# GET /cruds/new
def new
#crud = #model.new
render template: 'cruds/new'
end
# GET /cruds/1/edit
def edit
render template: 'cruds/edit'
end
# POST /cruds
# POST /cruds.json
def create
#crud = #model.new(crud_params)
respond_to do |format|
if #crud.save
format.html { redirect_to [:master, #crud], notice: 'Crud was successfully created.' }
format.json { render action: 'show', status: :created, location: #crud }
else
format.html { render action: 'new' }
format.json { render json: #crud.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /cruds/1
# PATCH/PUT /cruds/1.json
def update
respond_to do |format|
if #crud.update(crud_params)
format.html { redirect_to [:master, #crud], notice: 'Crud was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #crud.errors, status: :unprocessable_entity }
end
end
end
# DELETE /cruds/1
# DELETE /cruds/1.json
def destroy
#crud.destroy
respond_to do |format|
format.html { redirect_to action: :index }
format.json { head :no_content }
end
end
def import
#model.import(params[:file])
redirect_to root_url, notice: "Products imported."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_crud
#crud = #model.find_by_id(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def crud_params
params[#hash].permit(#model.attribute_names)
end
end
I'm very confused about this error, can someone tell me any solution?
You're iterating over attribute names of #model, and reading them on #crud. #model has an attribute named id; #crud is nil. Either both should be same (probably), or in the somewhat unusual case that you do mean to read #model's attributes off of #crud, you need to check where #crud is assigned.
Your code is a bit confusing, some small comments:
model.find_by_id(params[:id])
Should be:
model.find(params[:id])
Next I would make sure that your view actually passes params[:id] to the view and not something like crud_id. If the latter is the case you should call this:
model.find_by(crud_id: params[:crud_id])
Lastly, I think you should add :file to your strong parameters.
Related
I'm looking for make draw lesson with a differente gallery link with each lessons for the users can uplaod there work in.
I have a controller, model and view named "lesson" and the linked gallery is "illst_repot"
I have linked the illust_repot with references.
lesson:references and user:references
I have no problem on the index page, but I have a problem on new page
undefined method `illust_reports_path'
localhost:3000/lessons/1/illust_reports/new
This is my routes
lesson_illust_reports GET /lessons/:lesson_id/illust_reports(.:format) illust_reports#index
POST /lessons/:lesson_id/illust_reports(.:format) illust_reports#create
new_lesson_illust_report GET /lessons/:lesson_id/illust_reports/new(.:format) illust_reports#new
edit_lesson_illust_report GET /lessons/:lesson_id/illust_reports/:id/edit(.:format) illust_reports#edit
lesson_illust_report GET /lessons/:lesson_id/illust_reports/:id(.:format) illust_reports#show
PATCH /lessons/:lesson_id/illust_reports/:id(.:format) illust_reports#update
PUT /lessons/:lesson_id/illust_reports/:id(.:format) illust_reports#update
DELETE /lessons/:lesson_id/illust_reports/:id(.:format) illust_reports#destroy
lessons GET /lessons(.:format) lessons#index
POST /lessons(.:format) lessons#create
new_lesson GET /lessons/new(.:format) lessons#new
edit_lesson GET /lessons/:id/edit(.:format) lessons#edit
lesson GET /lessons/:id(.:format) lessons#show
PATCH /lessons/:id(.:format) lessons#update
PUT /lessons/:id(.:format) lessons#update
DELETE /lessons/:id(.:format) lessons#destroy
routes.rb
resources :lessons do
resources :illust_reports
end
illust_reports_controller.rb
class IllustReportsController < ApplicationController
before_action :set_illust_report, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
before_action :illust_report_owner, only: [:edit, :update, :destroy]
# GET /illust_reports
# GET /illust_reports.json
def index
#illust_reports = IllustReport.all
end
def illust_report_owner
unless #illust_report.user_id == current_user.id
flash[:notice] = "Accès refusé car tu n'es pas l'auteur de ce billet."
redirect_to illust_reports_path
end
end
# GET /illust_reports/1
# GET /illust_reports/1.json
def show
end
# GET /illust_reports/new
def new
#illust_report = current_user.illust_reports.build
end
# GET /illust_reports/1/edit
def edit
end
# POST /illust_reports
# POST /illust_reports.json
def create
#illust_report = current_user.illust_reports.build(illust_report_params)
respond_to do |format|
if #illust_report.save
format.html { redirect_to #illust_report, notice: 'Illust report was successfully created.' }
format.json { render :show, status: :created, location: #illust_report }
else
format.html { render :new }
format.json { render json: #illust_report.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /illust_reports/1
# PATCH/PUT /illust_reports/1.json
def update
respond_to do |format|
if #illust_report.update(illust_report_params)
format.html { redirect_to #illust_report, notice: 'Illust report was successfully updated.' }
format.json { render :show, status: :ok, location: #illust_report }
else
format.html { render :edit }
format.json { render json: #illust_report.errors, status: :unprocessable_entity }
end
end
end
# DELETE /illust_reports/1
# DELETE /illust_reports/1.json
def destroy
#illust_report.destroy
respond_to do |format|
format.html { redirect_to illust_reports_url, notice: 'Illust report was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_illust_report
#illust_report = IllustReport.find_by(id: params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def illust_report_params
params.require(:illust_report).permit(:title, :image, :commentaire, :lesson_id, :user_id)
end
end
_form.html.erb
<%= form_for(#illust_report) do |f| %>
<% if #illust_report.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#illust_report.errors.count, "error") %> prohibited this illust_report from being saved:</h2>
<ul>
<% #illust_report.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
The model are linked.
I'm beginner with rails, I have searched a long time on google, and stack, and make many test before write this message.
Where is the problem ?
Thank you very much for your time.
undefined method `illust_reports_path'
You have nested resources. So the form_for should look like below
<%= form_for [#lesson, #illust_report] do |f| %>
and change the illust_reports_controller's new method like below
def new
#lesson = Lesson.find(params[:lesson_id])
#illust_report = current_user.illust_reports.build
end
Does lesson_illust_reports_path work?
As your illust_report resource is namespaced inside your lessons resource, you have to prepend lesson.
Evening everyone! I am having an issue with my controller, it is returning undefined methodresidents' for nil:NilClass` when i try to render the page.
i am trying to populate a new resident to a specific site.
my sites_controller.rb looks as follows. -- I created the method site_resident to handle this. (thirs method from the top of the page.
class SitesController < ApplicationController
before_action :authenticate_user!
before_action :set_site, only: [:show, :edit, :update, :destroy]
# GET /sites
# GET /sites.json
def index
#sites = Site.all
end
# GET /sites/1
# GET /sites/1.json
def show
end
def site_resident
#resident = #site.residents.new
end
# GET /sites/new
def new
#site = Site.new
end
# GET /sites/1/edit
def edit
end
# POST /sites
# POST /sites.json
def create
#site = Site.new(site_params)
respond_to do |format|
if #site.save
format.html { redirect_to #site, notice: 'Site was successfully created.' }
format.json { render :show, status: :created, location: #site }
else
format.html { render :new }
format.json { render json: #site.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /sites/1
# PATCH/PUT /sites/1.json
def update
respond_to do |format|
if #site.update(site_params)
format.html { redirect_to #site, notice: 'Site was successfully updated.' }
format.json { render :show, status: :ok, location: #site }
else
format.html { render :edit }
format.json { render json: #site.errors, status: :unprocessable_entity }
end
end
end
# DELETE /sites/1
# DELETE /sites/1.json
def destroy
#site.destroy
respond_to do |format|
format.html { redirect_to sites_url, notice: 'Site was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_site
#site = Site.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def site_params
params.require(:site).permit(:call_sign, :address, :unit_number, :city, :prov, :postal_code, :ec1_n, :ec1_t, :ec1_t, :ec2_n, :ec2_t, :ec2_e, :ec3_n, :ec3_t, :ec3_e, :lat, :lng)
end
end
My routes.rb file look like this :
Rails.application.routes.draw do
root 'home#index'
resources :residents
resources :sites do
member do
get :site_resident
end
end
devise_for :users
im sure its something small that im overlooking but i cant seem to wrap my head around it. thanks for the help in advance!
You need to add :site_resident to your array for the before_action :set_site.
The line should read:
before_action :set_site, only: [:show, :edit, :update, :destroy, : site_resident]
I am trying to set the associations . By now I have both models Post and Comment with their associations (in theory).
The problem is that when I try to see the comment of a specific post it says that such method does not exist.
I am sure forgetting something kinda obvious but I can't figure it out.
Thanks in advance!
MODELS
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
end
class Comment < ActiveRecord::Base
belongs_to :post
end
And i got the error on rails console:
p = Post.all
p[0].comments
"undefined method comments for post"
Controllers:
Coment:
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# GET /comments
# GET /comments.json
def index
#comments = Comment.all
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
#comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
#comment = Comment.new(comment_params)
respond_to do |format|
if #comment.save
format.html { redirect_to #comment, notice: 'Comment was successfully created.' }
format.json { render action: 'show', status: :created, location: #comment }
else
format.html { render action: 'new' }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if #comment.update(comment_params)
format.html { redirect_to #comment, notice: 'Comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
#comment.destroy
respond_to do |format|
format.html { redirect_to comments_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
#comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:post_id, :body)
end
end
Post:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
#posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
#post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #post }
else
format.html { render action: 'new' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
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(:title, :body)
end
end
Try this
has_many :comments, :class_name => "Comment", :foreign_key => :post_id
I think this is because you're trying to pull all comments for all posts, try this in rails console. This will only pull the first Post and then its related comment.
p = Post.first
p.comments
Are you getting an error on the view as well? If so, can you post the code from the view in question.
I've done a simple project about paper_trail, however I encountered a problem. When I created or updated completely there were two message show in the view:
Product was successfully created. undo
Product was successfully created. <a data-method="post" href="/versions/148/revert" rel="nofollow">undo</a>
here is my controller file:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
# GET /products
# GET /products.json
def index
#products = Product.order(:name)
respond_to do |format|
format.html
format.csv { send_data #products.to_csv }
end
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
#product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: "Product was successfully created. #{undo_link}" }
format.json { render action: 'show', status: :created, location: #product }
else
format.html { render action: 'new' }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to product_url, notice: 'Product was successfully updated.' "#{undo_link}" }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: "Successfully destroyed product. #{undo_link}" }
format.json { head :no_content }
end
end
def import
Product.import(params[:file])
redirect_to root_url, notice: "Products imported."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
#product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :price)
end
def undo_link
view_context.link_to("undo", revert_version_path(#product.versions.scoped.last), :method => :post)
end
end
and the layout file here:
<!DOCTYPE html>
<html>
<head>
<title>Store</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="container">
<% flash.each do |name, msg|%>
<%= content_tag :div, raw(msg), :id => "flash_#{name}" %>
<% end %>
<%= yield %>
</div>
</body>
</html>
I expect to show message once, however it show twice, so please tell me where my errors?
You are calling a flash message in both your create action and your view.
Create action:
format.html { redirect_to #product, notice: "Product was successfully created. #{undo_link}" }
View:
<% flash.each do |name, msg|%>
<%= content_tag :div, raw(msg), :id => "flash_#{name}" %>
<% end %>
The first gives:
Product was successfully created. undo
The latter show all raw output because of raw(msg):
Product was successfully created. <a data-method="post" href="/versions/148/revert" rel="nofollow">undo</a>
I need help with the follow problem. I have been looking at similar questions but am still unable to find the solution to the problem. I am currently having this problem.
NoMethodError in Courses#edit
undefined method `model_name' for #Class:0xb5068474
the error seems to be with this line of code.
<%= simple_fields_for #lesson do |f| %>
<%= f.input :lesson_name %>
<%end%>
The lesson database is link to the course database, where course has_many lessons and lesson belongs_to course. I think that the problem may be due to my controller code. I am relatively new and not very sure about how to go about solving this problem.
I am able to go to create page to create that relation but nothing else is saved inside the database but the id of course and lesson. However, when i go to the edit page for course, this error pops out.
courses_controller.rb
class CoursesController < ApplicationController
before_action :set_course, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, :except => [:show,:index]
# GET /courses
# GET /courses.json
def index
#courses = Course.all
end
# GET /courses/1
# GET /courses/1.json
def show
end
# GET /courses/new
def new
#course = Course.new
#lesson = #course.lessons.build
end
# GET /courses/1/edit
def edit
#course = Course.find(params[:id])
#lesson = #course.lessons
end
# POST /courses
# POST /courses.json
def create
#course = Course.new(course_params)
#course.lessons.new(lesson_params)
respond_to do |format|
if #course.save
format.html { redirect_to #course, notice: 'Course was successfully created.' }
format.json { render action: 'show', status: :created, location: #course }
else
format.html { render action: 'new' }
format.json { render json: #course.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /courses/1
# PATCH/PUT /courses/1.json
def update
respond_to do |format|
if #course.update(course_params)
#course.lesson.update_attributes(lesson_params)
#course.staff_ids=params[:course][:staff_ids]
format.html { redirect_to #course, notice: 'Course was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #course.errors, status: :unprocessable_entity }
end
end
end
# DELETE /courses/1
# DELETE /courses/1.json
def destroy
#course.destroy
respond_to do |format|
format.html { redirect_to courses_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_course
#course = Course.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def course_params
params.require(:course).permit(:course_code, :course_name, :year_of_study, :discipline, :Acad_unit, :cohort_size, :remark)
end
def lesson_params
params.require(:lesson).permit(:lesson_type, :lesson_name, :num_lesson, :frequency)
end
end
lesson.rb
class Lesson < ActiveRecord::Base
belongs_to :course
end
course.rb
class Course < ActiveRecord::Base
validates_presence_of :course_code, :course_name
validates_uniqueness_of :course_code, :course_name
validates :year_of_study, :Acad_unit, :cohort_size, :numericality => { :greater_than => 0}
has_and_belongs_to_many :staffs, join_table: :scheduleCourse
has_many :lessons, dependent: :destroy
end
Please give me some advise. Thanks in advance
The error that you get:
undefined method `model_name` for #Class:0xb5068474
Means that simple_fields_for method is doing something like #lesson.class.model_name, which is ok when object you pass (#lesson) is an instance of ActiveRecord::Base, but in edit action you define #lesson
#lesson = #course.lessons
As a relation, so it is not one Lesson instance, it is internal ActiveRecord object for storing relations. If for instance you call first on it:
#lesson = #course.lessons.first
This should work. Or you can iterate through collection of lessons in view to create form for each one of them, it depends on what you want to do.