undefined local variable or method `employee_params' for #<EmployeesController:0x37b7f68> - ruby-on-rails-3

Controller
class EmployeesController < ApplicationController
def new
#employee = Employee.new
end
def create
#employee = Employee.new(employee_params)
if #employee.save
format.html { redirect_to #employee, notice: 'Status was successfully created.' }
else
render "new"
end
private
def employee_params
params.require(:employee).permit(:firstname, :lastname)
end
end
end
models/employee.rb
class Employee < ActiveRecord::Base
attr_accessor :firstname, :lastname
end
new.html.erb
Enter new information
<hr>
<%= form_for #employee do |f| -%>
FirstName:<%= f.text_field :firstname%><br />
LastName :<%= f.text_field :lastname%><br />
<%= f.submit "submit"%>
<% end %>
my output:
http://grab.by/DA3A
this is my screenshot..please help me to figure out this problem.

You mistyped it (in your controller) bro. It should be like:
class EmployeesController < ApplicationController
def new
#employee = Employee.new
end
def create
#employee = Employee.new(employee_params)
if #employee.save
format.html { redirect_to #employee, notice: 'Status was successfully created.' }
else
render "new"
end
end
private
def employee_params
params.require(:employee).permit(:firstname, :lastname)
end
end
I don't think I need to elaborate this :)

Related

Rails passing value from view to controller

I tried to generate some random no of string. But the number of generated string is decided by user.
User will enter the number of string in view page and it will be received by controller and Faraday will generate the number of paragraph user want, But i am not able to do this.
ApplicationController
class ApplicationController < ActionController::Base
private
def baconipsum
#baconipsum ||= Faraday.new("https://baconipsum.com/") do |f|
f.response :json
end
end
end
ArticlesController:
# frozen_string_literal: true
class ArticlesController < ApplicationController
http_basic_authenticate_with name: 'deba', password: '12345', except: %i[index show]
before_action :set_value
def index
#articles = Article.all
end
def show
#bacon = baconipsum.get("api/", type: 'all-meat',paras: #value).body
#article = Article.find(params[:id])
end
def showData
#value = params[:value] #tried to get value from user and using set_value function tried to make it global, so that show method will get the new value of value from showData method.
end
def create
#article = Article.new(article_params)
if #article.save
redirect_to #article
else
render 'new'
end
end
def update
#article = Article.find(params[:id])
if #article.update(article_params)
redirect_to #article
else
render 'edit'
end
end
def delete
#article = Article.find(params[:id])
#article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
def set_value
#value = 0
end
end
views from getting user input:
<%= form_for:article do %>
<label for="value">Value</label>
<%= text_field_tag :value %>
<%= submit_tag "Submit" %>
<% end %>
routes.rb
# frozen_string_literal: true
Rails.application.routes.draw do
root 'articles#index'
delete 'articles/:id', to: 'articles#delete'
resources :articles do
resources :comments
end
end

Rails File Upload with Nested Attribute, "file_content" is blank when file gets saved

I've been following this tutorial to upload file to db Rails File Upload
.
The problem is file_contents field is blank when file gets saved and I'm uploading file with nested_attributes.
subject.rb //model
class Subject < ApplicationRecord
has_many :documents
accepts_nested_attributes_for :documents
end
document.rb //model
class Document < ApplicationRecord
belongs_to :subject
def initialize(params = {})
#file = params.delete(:file)
super
if #file
self.filename = sanitize_filename(#file.original_filename)
self.content_type = #file.content_type
self.file_contents = #file.read
end
end
private
def sanitize_filename(filename)
return File.basename(filename)
end
end
_form.html.erb //view
<%= form_for(subject) do |f| %>
.......
<div class="uk-form-row">
<%= f.fields_for :documents do |d| %>
<%= d.file_field :file, class:"uk-width-1-1 uk-form-large", placeholder:"upload document" %>
<% end %>
</div>
............
<% end %>
subjects_controller.rb
def new
#subject = Subject.new
#subject.documents.build
end
def create
#subject = Subject.new(subject_params)
respond_to do |format|
if #subject.save
format.html { redirect_to #subject, notice: 'Lesson was successfully created.' }
else
format.html { render :new }
end
end
end
private
def subject_params
params.require(:subject).permit(:title, documents_attributes:[:file])
end
Am I in the right way ? What am I missing ? I need your thoughts.
Thanks,

undefined method 'random_generate' for #<Generator:0x4cce798>

i have a method which i wan to call from my controller. What i've read in stacks are most of them said to include the method in applicationController.Therefore i did. But still i got the following error. Can some1 help me ? New to rails
APPLICATION CONTROLLER :
helper_method :random_generate
def random_generate
length = #generator.primer_length
chars = 'ATGC'
seq = ''
length.times { seq << chars[rand(chars.size)] }
#generator.random_primer_generated= seq
end
CONTROLLER :
class GeneratorsController < ApplicationController
before_action :set_generator, only: [:show, :edit, :update, :destroy]
after_action :generate_option, only: [:create,:edit]
helper_method :random_primer_generated
# GET /generators
# GET /generators.json
def index
#generators = Generator.all
end
# GET /generators/1
# GET /generators/1.json
def show
end
# GET /generators/new
def new
#generator = Generator.new
end
# GET /generators/1/edit
def edit
end
# POST /generators
# POST /generators.json
def create
#generator = Generator.new(generator_params)
respond_to do |format|
if #generator.save
format.html { redirect_to #generator, notice: 'Generator was successfully created.' }
format.json { render action: 'show', status: :created, location: #generator }
else
format.html { render action: 'new' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /generators/1
# PATCH/PUT /generators/1.json
def update
respond_to do |format|
if #generator.update(generator_params)
format.html { redirect_to #generator, notice: 'Generator was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /generators/1
# DELETE /generators/1.json
def destroy
#generator.destroy
respond_to do |format|
format.html { redirect_to generators_url }
format.json { head :no_content }
end
end
def generate_option
if params[:choice] == 'Randomly'
#generator.random_generate
end
#generator.save!
end
private
# Use callbacks to share common setup or constraints between actions.
def set_generator
#generator = Generator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def generator_params
params.require(:generator).permit(:primer_length, :choice, :random_primer_generated)
end
end
VIEW:
<%= form_for (#generator ) do |f| %>
<% if #generator.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#generator.errors.count, "error") %> prohibited this generator from being saved:</h2>
<ul>
<% #generator.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<label>Primer Length</label><br>
<%= f.number_field :primer_length %>
</div>
<label>Selection :</label><br>
<label>Randomly</label>
<%= radio_button_tag(:choice, 'Randomly')%>
<%= button_to('Generate',generate_option_generator_path(#generator))%>
<% end %>
If you want to call random_generate on the #generator, you have to implement that method in the Generator class. What you have done is implemented it in the ApplicationController, so you would have to call it without referencing to #generator.
Also, I think calling helper_method :random_generate is only required if you want to call random_generate as a controller method inside of a view.

Create a new perfil inside a persons _controller

I'm in rails 3.2.2
I'm trying to create a new perfil inside a persons_controller
Models symplify:
class Person < ActiveRecord::Base
belongs_to :perfil, :foreign_key=>'perfil_id'
class Perfil < ActiveRecord::Base
has_one :person
I created ad_perfil inside persons_controller
def ad_perfil
#person = Person.find(params[:id])
#perfil = Perfil.new
end
and I supused 2 ways:
1) using f.submit
ad_perfil.html.erb:
<%= #person.nombre %>
<%= form_for(#perfil) do |f| %>
<div class="field">
<%= f.label :nombre %><br />
<%= f.text_field :nombre %>
</div>
<%= f.submit %>
<% end %>
this redirect me to create to perfils_controller and there I can't get #persona:
def create
#perfil = Perfil.new(params[:perfil])
#person = Person.find(params[:id])
respond_to do |format|
if #perfil.save
format.html { redirect_to #perfil, notice: 'Perfil was successfully created.' }
format.json { render json: #perfil, status: :created, location: #perfil }
#person.perfil_id = #perfil.id
#person.save
2) I created a new def in persons_controller:
def new_perf
#perfil = Perfil.new(params[:perfil])
#person = Person.find(params[:id])
if #perfil.save
#person.perfil = #perfil
#person.update_attributes(params[:person])
redirect_to #person
end
end
and I tryed change f.submit for:
<td><%= link_to 'New Perfil', :action => "new_perf", :id=> #person.id %></td>
and it created a new perfil without any field and of course don save perfil.id inside #person.perfil_id.
Is possible that I'm trying? Is there any other methods? I guess that will be more easy puting perfil.person_id instead person.perfil_id but I want access to person.perfil later.
I hope you understand me, and maybe I don't understand "submit" very well. any suggestions? thanks!

Polymorphic Comments with Ancestry Problems

I am trying to roll together two Railscasts: http://railscasts.com/episodes/262-trees-with-ancestry and http://railscasts.com/episodes/154-polymorphic-association on my app.
My Models:
class Location < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
end
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
My Controllers:
class LocationsController < ApplicationController
def show
#location = Location.find(params[:id])
#comments = #location.comments.arrange(:order => :created_at)
respond_to do |format|
format.html # show.html.erb
format.json { render json: #location }
end
end
end
class CommentsController < InheritedResources::Base
def index
#commentable = find_commentable
#comments = #commentable.comments.where(:company_id => session[:company_id])
end
def create
#commentable = find_commentable
#comment = #commentable.comments.build(params[:comment])
#comment.user_id = session[:user_id]
#comment.company_id = session[:company_id]
if #comment.save
flash[:notice] = "Successfully created comment."
redirect_to :id => nil
else
render :action => 'new'
end
end
private
def find_commentable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
end
In my locations show view I have this code:
<%= render #comments %>
<%= render "comments/form" %>
Which outputs properly. I have a _comment.html.erb file that renders each comment etc. and a _form.html.erb file that creates the form for a new comment.
The problem I have is that when I try <%= nested_comments #comments %> I get undefined method 'arrange'.
I did some Googling and the common solution to this was to add subtree before the arrange but that throws and undefined error also. I am guessing the polymorphic association is the problem here but I am at a loss as to how to fix it.
Dumb mistake... forgot to add the ancestry gem and required migration which I thought I had already done. The last place I checked was my model where I eventually discovered my error.