force strong parameter value - ruby-on-rails

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

Related

Ruby on Rails: video object tags not updating

Video objects have tags (via acts-as-taggable-on gem). The update function works for all of the video attributes except for tags. I'm not sure why. Any insight would be greatly appreciated.
views/videos/_new.html.erb
<div class="">
<%= f.label(:tag_list, "Tags (separated by commas)") %></br >
<%= f.text_field :tag_list, multiple: true, class: "form-control" %>
</div>
videos_controller.rb
def edit
#video = Video.find(params[:id])
#post = #video.post
end
def update
#video = Video.find(params[:id])
#post = #video.post
#video.update_attributes(video_params)
if current_user == #video.user
if #video.save
flash[:notice] = "Video successfully updated!"
redirect_to video_path(#video)
else
flash[:notice] = "Video was not updated."
#errors = #video.errors.full_messages.join(", ")
flash[:alert] = #errors
render action: "edit"
end
else
flash[:notice] = "Only OP can edit video"
refresh :edit
end
end
private
def video_params
params.require(:video).permit(
:title,
:url,
:tag_list,
)
end
Looks like your video_params is filtering it.
Try
params.require(:video).permit(
:title,
:url,
tag_list: []
)

Image does not upload carriewave / simple_form / Rails

When I try to create new record image doesn't upload but when I try to edit everything working fine. No errors, after create I have nil in DB after Update I have a link to a picture and everything fine. What I did wrong? Before I used form_for and everything was working fine, then I change form_for to simple_form. Maybe problem here?
Controller:
def create
#byebug
#bar = current_user.bars.new(bar_params)
if #bar.save
UserMailer.admin_verify_email(#bar.id).deliver_now
flash[:success] = t(".bar_created_successfully")
# if admin_verified add bar to the search bos
if #bar.admin_verified
Barsearchsuggestion.index_bar(#bar)
redirect_to bars_path(my_bar: true)
else
redirect_to bar_status_user_path(current_user)
end
else
flash.now[:alert] = #bar.errors.full_messages
render 'users/add_bar'
end
end
def edit
#bar = Bar.find(params[:id])
end
def update
#bar = Bar.find(params[:id])
if #bar.update_attributes(bar_params)
flash[:success] = "Bar updated."
redirect_to bar_status_user_path(current_user)
else
flash.now[:error] = I18n.t("simple_form.error_notification.default_message")
# flash[:error] = #bar.errors.to_array
render 'edit'
end
end
private
def bar_params
params.require(:bar).permit!
end
View:
=simple_form_for(#bar) do |f|
.form-group
= f.label :profile_picture
= t(".profile_picture_ext_html")
%br
= image_tag f.object.profile_picture.bar_detail.url
= f.file_field :profile_picture, class: 'form-control'
Model:
mount_uploader :profile_picture, BarPictureUploader
You have to permit the profile_picture parameter like this:
def bar_params
params.require(:bar).permit(:profile_picture)
end
Try setting multipart: true in your form

Using attributes from one model in another Rails

I apologize that this is such a simplistic question, but I've been struggling with it for a while.
I have two related models - Tour & Reservation. "Tour" has a "days" attribute. I want to list the days in a select tag for the user to choose from in my "Reservation" view
I thought this might work:
(Reservations controller) #tour_days = Tour.where(:days => params[:days])
(Reservations #new) = f.select :days, #tours_days
However, I'm receiving the error undefined methoddays' `
class Reservation < ActiveRecord::Base
belongs_to :tour
end
class Tour < ActiveRecord::Base
has_many :reservations
end
.
class ReservationsController < ApplicationController
def index
end
def new
#reservation = Reservation.new
#tour = Tour.find(params[:tour_id])
#tour_days = Tour.where(:days => params[:days])
end
def create
#tour = Tour.find(params[:tour_id])
if #reservation.update_attribute(:t_shirt, params[:t_shirt]) == true || #reservation.update_attribute(:hat, params[:hat]) == true
#tour.amount = #tour.amount + 15
else
#tour.amount = #tour.amount
end
#reservation = Reservation.new(reservation_params)
if #reservation.save
Stripe.api_key = ENV["STRIPE_SECRET_KEY"]
Stripe::Charge.create(
:amount => #tour.amount, # amount in cents, again
:currency => "usd",
:card => params[:stripeToken]
)
flash[:success] = "Your reservation has been booked for #{#reservation.passengers} person(s). Please save this info."
redirect_to new_tour_reservation_path(#tour)
else
render 'new'
end
end
private
def reservation_params
params.require(:reservation).permit(:passengers, :t_shirt, :hat)
end
end
.
class ToursController < ApplicationController
def index
#tours = Tour.all
end
def new
#tour = Tour.new
end
def create
#tour = Tour.new(tours_params)
if #tour.save
flash[:success] = "Tour #{#tour.name} has been successfully added."
redirect_to new_tour_path
else
flash[:error] = "The tour #{#tour.name} was not successfully saved. Please try again"
render 'new'
end
end
def show
#tour = Tour.find_by(id: params[:id])
#reservation = Reservation.new
end
def edit
#tour = Tour.find_by(id: params[:id])
end
def update
#tour = Tour.find_by(id: params[:id])
if #tour.update_attributes(tours_params)
flash[:success] = "#{#tour.name} has been successfully updated."
redirect_to tours_path
else
flash[:error] = "#{#tour.name} has not been updated. Please try again."
render 'edit'
end
end
def delete
#tour = Tour.find_by(id: params[:id])
end
def destroy
#tour = Tour.find_by(id: params[:id])
if #tour.destroy
flash[:success] = "The #{#tour.name} has been successfully deleted."
redirect_to tours_path
else
flash[:error] = "The #{#tour.name} has not been deleted. Please try again."
render 'edit'
end
end
private
def tours_params
params.require(:tour).permit(:name, :amount, :days)
end
end
.
= bootstrap_form_for([:tour, #reservation], html: { class: 'form-horizontal', id: 'payment-form'}) do |f|
= f.alert_message 'Please fix the errors below:'
= f.select :passengers, options_for_select( (1..10).map { |n| n %1 == 0 ? n.to_i : n } )
= f.select :days, #tours_days
%fieldset.credit_card
%span.payment-errors
.control-group
= label_tag :card_number, 'Credit card number:', class: 'control-label'
.controls
= text_field_tag :card_number, nil, name: nil, class: 'span3', data: {stripe: 'number'}
.control-group
= label_tag :security_code, 'Security code:', class: 'control-label'
.controls
= text_field_tag :security_code, nil, name: nil, class: 'span3', data: {stripe: 'cvc'}
.control-group
= label_tag :exp_date, 'Expiration:', class: 'control-label'
.controls
= select_month(Date.today, {add_month_numbers: true}, class: 'span2', data: {stripe: 'exp-month'})
= select_year(Date.today.year, {start_year: Date.today.year, end_year: Date.today.year + 4}, class: 'span1', data: {stripe: 'exp-year'})
%fieldset.actions.control-group
.controls
= f.submit 'Sign up'
consider using accepts_nested_attributes_for
Create another model to encapsulate the days. Then associate it with the Reservation model.
class Reservation < ActiveRecord::Base
belongs_to :tour
has_and_belongs_to_many :days
accepts_nested_attributes_for :days, allow_destroy: true
end
class Day < ActiveRecord::Base
has_and_belongs_to_many :reservations
end
The Day model will have one attribute: name which will hold the names of the seven days
class ReservationsController < ApplicationController
def create
#reservation = Reservation.new(reservation_params)
if #reservation.save
redirect_to #save
else
render :new
end
end
private
#add the `days_attributes` to the `reservations_params`
def reservation_params
params.require(:reservation).permit(:passengers, :t_shirt, :hat, days_attributes[:id, name])
end
end
then in new.html.erb when you are creating reservations, you can get a drop down to select specific days. you can do something like:
f.select :days
if you opt to use nested_forms, you'd have to use boostrap_nested_form_for as the documentation suggests.

flash[:notice] displaying extra character

I'm having a strange issue where an extra/random character ("0") is being displayed with my flash[:notice]. I can't figure out where it's coming from.
Controller:
def edit
#subject = Subject.find_by(id: params[:id])
end
def update
#subject = Subject.find_by(id: params[:id])
if #subject.update_attributes(strong_params)
flash[:notice] = 'Subject has been updated'
redirect_to action: 'show', id: #subject.id
else
render 'edit'
end
end
def delete
#subject = Subject.find_by(id: params[:id])
end
private
def strong_params
params.require(:subject).permit(:name, :position, :visible)
end
View:
= if !flash[:notice].blank?
.notice
= flash[:notice]
%h2 Update subject
= form_for :subject, :url => {action: 'update', id: #subject.id} do |f|
= f.label :name, 'Name:'
= f.text_field :name
= f.label :position, 'Position:'
= f.text_field :position
= f.label :visible, 'Visible:'
= f.text_field :visible
= f.submit 'Update Subject'
%h2
Are you sure you want to delete the subject - #{#subject.name}
= link_to 'Yes, delete!', action: 'destroy', id: #subject.id
So, as it turned out the answer is: change 'loud' haml = if !flash[:notice].blank? to 'silent' - if !flash[:notice].blank?.

Can't write params to Model. 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

Resources