Can't write params to Model. Ruby on Rails - ruby-on-rails

I trying write params to Company model. But I have error undefined method `model_name' for NilClass:Class in this point = simple_form_for #company, url: update_settings_company_path do |f|. Where I must set #company?
Controller
def change_settings
#vacation_days = current_company.vacation_days
#illnes_days = current_company.illnes_days
end
def update_settings
if #company.update(company_params)
redirect_to account_company_path, notice: t('company.settings_changed')
else
render action: 'change_settings'
end
end
private
def company_params
params.require(:company).permit(:vacation_days, :illnes_days)
end
View
.company_settings
= simple_form_for #company, url: update_settings_company_path do |f|
= f.error_notification
= f.input :vacation_days
= f.input :illnes_days
%br
= f.submit t('common.save'), class: 'btn'
= link_to t('common.back'), account_company_path, class: 'btn'
routes
resource :company, only: :all do
get :change_settings
post :update_settings
patch :update_settings
end
What's wrong? Help me please

You don't set #company instance variable in both your actions. You can do it using before_filter, like this:
before_filter :find_company
def change_settings
#vacation_days = current_company.vacation_days
#illnes_days = current_company.illnes_days
end
def update_settings
if #company.update(company_params)
redirect_to account_company_path, notice: t('company.settings_changed')
else
render action: 'change_settings'
end
end
private
def company_params
params.require(:company).permit(:vacation_days, :illnes_days)
end
def find_company
#company = current_company
end

Try this instead, You need to set the instance variable before you use it. By default the instance variable is set to nil.
def update_settings
#company = current_company
if #company.update(company_params)
redirect_to account_company_path, notice: t('company.settings_changed')
else
render action: 'change_settings'
end
end

Related

How to give ID to other controller: no implicit conversion of Symbol into Integer error

I want to give an id of "paper model" to "schedules model" and create a note_id.
However an error message "no implicit conversion of Symbol into Integer" shows.
Can someone help me to solve this problem?
papers/index.html.erb
<%= link_to "Go to Schedule", new_schedule_path(id: paper.id) %>
routes.rb
get '/schedules/new/:id', to: 'schedules#new', as: :schedules_new
schedules_controller
class ActionsController < ApplicationController
before_action :authenticate_user!
before_action :set_schedule, only: [:edit, :update, :destroy]
def new
#schedule = Schedule.new
end
def create
#schedule = Schedule.new(schedule_params)
#schedule.user_id = current_user.id
#schedule.note_id = params[:id]
if #schedule.save
redirect_to schedules_path, notice: "A schedule was saved!"
end
end
def index
#schedules = Schedule.all
end
def update
end
def delete
end
Private
def schedule_params
params.require(:schedule).permit(:note_id, :user_id, :params[:id])
end
def set_schedule
#schedule = Schedule.find(params[:id])
end
end
params
=> "31", "controller"=>"schedules", "action"=>"new"} permitted: false>
You are not even using shedule_params values, as you override them afterwards ......
Then you could create empty Schedule object and then assign values ...
def create
#schedule = Schedule.new
#schedule.user_id = current_user.id
#schedule.note_id = params[:id]
if #schedule.save
redirect_to schedules_path, notice: "A schedule was saved!"
end
end
Or if I am correct with your relations, it could be also
def create
#schedule = current_user.schedules.new
#schedule.note_id = params[:id]
if #schedule.save
redirect_to schedules_path, notice: "A schedule was saved!"
end
end
I could solve this problem as follow.
Controller:
def new
#schedule = Schedule.new
#schedule.note_id = params[:id]
end
def create
#schedule = Schedule.new(schedule_params)
#schedule.user_id = current_user.id
if #schedule.save
redirect_to schedules_path, notice: "A schedule was saved!"
else
render 'new'
end
end
.
.
.
Private
def schedule_params
params.require(:schedule).permit(:note_id, :user_id)
end
Add into View
<%= f.hidden_field :id, :value => #schedule.note_id %>

Ruby on Rails Partial Locals

On my site users can both take and retake a quiz, meaning I will use the quiz code in a partial view that will be used both by the the edit and new views.
To render my partials I have in my new view:
<%= render partial: "quiz", locals: { url: quiz_bs_path } %>
and in my edit view:
<%= render partial: "quiz", locals: { url: edit_quiz_b_path } %>
The link_to locations are from my rake routes results:
quiz_bs GET /quiz_bs(.:format) quiz_bs#index
POST /quiz_bs(.:format) quiz_bs#create
new_quiz_b GET /quiz_bs/new(.:format) quiz_bs#new
edit_quiz_b GET /quiz_bs/:id/edit(.:format) quiz_bs#edit
quiz_b GET /quiz_bs/:id(.:format) quiz_bs#show
PATCH /quiz_bs/:id(.:format) quiz_bs#update
PUT /quiz_bs/:id(.:format) quiz_bs#update
DELETE /quiz_bs/:id(.:format) quiz_bs#destroy
I am getting an Argument Error in my edit view saying First argument in form cannot contain nil or be empty. The line on which the error is being called is in my partial:
<%= form_for #quiz_bs, url: url, method: :post do |f| %>
My controller code is:
class QuizBsController < ApplicationController
before_action :require_sign_in
def show
#quiz_bs = QuizBs.find(params[:id])
end
def new
#quiz_bs = current_user.quiz_bs || current_user.build_quiz_bs
end
def create
#quiz_bs = QuizBs.new
#quiz_bs.bs01 = params[:quiz_bs][:bs01]
#quiz_bs.bs02 = params[:quiz_bs][:bs02]
#quiz_bs.bs03 = params[:quiz_bs][:bs03]
#quiz_bs.bs04 = params[:quiz_bs][:bs04]
#quiz_bs.bs05 = params[:quiz_bs][:bs05]
#quiz_bs.bs06 = params[:quiz_bs][:bs06]
#quiz_bs.user = current_user
if #quiz_bs.save
flash[:notice] = "Quiz results saved successfully."
redirect_to user_path(current_user)
else
flash[:alert] = "Sorry, your quiz results failed to save."
redirect_to welcome_index_path
end
end
def update
#quiz_bs = QuizBs.find(params[:quiz_bs])
#quiz_bs.assign_attributes(quiz_bs_params)
if #quiz_bs.save
flash[:notice] = "Post was updated successfully."
redirect_to user_path(current_user)
else
flash.now[:alert] = "There was an error saving the post. Please try again."
redirect_to welcome_index_path
end
end
private
def quiz_bs_params
params.require(:quiz_bs).permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
end
end
The model is:
class QuizBs < ActiveRecord::Base
before_save :set_bscode
def set_bscode
self.bscode = "#{self.bs01}#{self.bs02}#{self.bs03}-#{self.bs04}#{self.bs05}#{self.bs06}"
end
belongs_to :user
validates :user, presence: true
end
Any ideas where I am going wrong?
Keep it more simple, if you want to assign to current_user, create method will do for you.
Partial:
#new.html.erb
<%= render partial: "quiz", locals: {quiz: #quiz_bs, url: :quiz_bs } %>
#edit.html.erb
<%= render partial: "quiz", locals: {quiz: #quiz_bs, url: :quiz_b } %>
Form:
#_form.html.erb
<%= form_for quiz, url: url do |f| %>
Controller:
def new
#quiz_bs = QuisBs.new
end
You do not have edit action in controller, so rails assumes it being empty. Thus #quiz_bs is really nil
Add something like
def edit
#quiz_bs = QuizBs.find(params[:id])
end
also in your update action params[:quiz_bs] is most likely nil too and should be changed to params[:id]
It is not necessary to create RESTfull methods in controller but if you are using it as I can see #quiz_bs then you will have to initialize it first then you can use it, In your new view you initialize the #quiz_bs by
#quiz_bs = current_user.quiz_bs || current_user.build_quiz_bs
then you are using it.
and also routes which is generated for edit is needed an id i.e.
edit_quiz_b GET /quiz_bs/:id/edit(.:format) quiz_bs#edit
so you will have to pass #quiz_bs like
edit_quiz_b_path(#quiz_bs)
GOT it?
Some updates to your controller:
before_action :assign_quiz_bs, only: [:show, :edit, :update, :destroy]
#...
def update
#quiz_bs.update(quiz_bs_params)
# .... rendering ...
end
private
def assign_quiz_bs
#quiz_bs = Quiz.find(params[:id])
end
def quiz_bs_params
params.require(:quiz_bs).permit(:bs01, :bs02, :bs03, :bs04, :bs05, :bs06)
end

force strong parameter value

how to insert parameter value manually from controller,
this my controller
def new
#new_post = Post.new(params[:title])
end
def create
#new_post = Post.new(new_post_params)
if #new_post.save
flash[:success] = 'Post created!'
redirect_to home_url
else
flash[:danger] = #new_post.errors.full_messages.to_sentence
redirect_to home_url
end
end
private
def new_post_params
params.require(:post).permit(
:title,
poster_id: current_user.id,
poster_name: current_user.name
)
end
my form view like this
form_for #new_post do |f|
f.text_area :title
end
tired using this method, poster_id and poster_name still blank
def new_post_params
params.require(:post).permit(
:title,
poster_id: current_user.id,
poster_name: current_user.name
)
end
Try this
params.require(:post).permit(:title).merge(
{
poster_id: current_user.id,
poster_name: current_user.name
}
)
def new_post_params
params[:post][:poster_id] = current_user.id
params[:post][:poster_name] = current_user.name
params.require(:post).permit(
:title,
:poster_id,
:poster_name
)
end

Ruby on Rails - undefined local variable or method (strong parameters)

My app is using Rails 4.1.8 and I use Simple Form gem.
I get the undefined local variable or method error on the strong parameter definition under private and I don't understand the cause of this.
_form.haml.html
= simple_form_for #recipe, html: { multipart: true } do |f|
- if #recipe.errors.any?
#errors
%h2
= pluralize(#recipe.errors.count, "error")
prohibited this recipe from being saved
%ul
- #recipe.errors.full_messages.each do |msg|
%li= msg
= f.input :title
= f.input :serving
= f.input :prep
= f.input :cook
= f.button :submit
controller
class RecipesController < ApplicationController
def create
#recipe = Recipe.new(recipe_params)
if #recipe.save
redirect_to #recipe, notice: "Successfully created"
else
render 'new'
end
private
def recipe_params
params.require(:recipe).permit(:title, :serving, :prep, :cook)
end
end
end
And the submit button takes me to index route http://localhost:3000/recipes rather than
show http://localhost:3000/recipes/1
You didn't close create method properly. It should be:
class RecipesController < ApplicationController
def create
#recipe = Recipe.new(recipe_params)
if #recipe.save
redirect_to #recipe, notice: "Successfully created"
else
render 'new'
end
end
private
def recipe_params
params.require(:recipe).permit(:title, :serving, :prep, :cook)
end
end

How to use form partial for new and edit action with admin routes?

How should I setup url for that partial form to be able use it for create and update action.
Right now I have to set up two urls: admin_faculty_path for update and admin_faculties_path for create action.
# admin/faculties/_form.html.haml
= simple_form_for #faculty, html: { multipart: true, class: 'form-horizontal' } do |f|
= f.input :department_id, as: :hidden, input_html: { value: params[:department_id] } if params[:department_id]
= f.input :name
= f.input :name_en
= f.submit
Routes:
admin_faculties POST /admin/faculties(.:format) admin/faculties#create
new_admin_faculty GET /admin/faculties/new(.:format) admin/faculties#new
edit_admin_faculty GET /admin/faculties/:id/edit(.:format) admin/faculties#edit
admin_faculty GET /admin/faculties/:id(.:format) admin/faculties#show
PUT /admin/faculties/:id(.:format) admin/faculties#update
Controller:
# faculties_controller.rb
def new
#faculty = Faculty.new
end
def create
#faculty = Faculty.new(params[:faculty])
if #faculty.save
redirect_to admin_departments_path,
notice: t('activerecord.successful_save_data')
else
flash[:error] = t('activerecord.save_data_error_html')
render 'new'
end
end
def edit
#faculty = Faculty.find(params[:id])
end
def update
#faculty = Faculty.find(params[:id])
if #faculty.update_attributes(params[:faculty])
redirect_to admin_departments_path,
notice: t('activerecord.successful_save_data')
else
flash[:error] = t('activerecord.save_data_error_html')
render 'edit'
end
end
You need to pass the namespace to the form:
simple_form_for [:admin, #faculty]
See for example
Rails Routes Namespaces and form_for

Resources