I'm trying to set the title of a Page in my create action.
I would need to page.translation.title = params[:page][:title]
def create
#page = Page.new(params[:page])
#page.translation.title = params[:page][:title]
if #page.save
redirect_to admin_pages_path
else
render :new
end
end
Also tried
#translation = #page.translation.build(title: params[:page][:title])
from the console when I run:
p = Page.last
p.translation.title
=> nil -----> right now after its created, title is nil.
p.translation.title = "foo"
=> "foo"
This is what I what in my create action. any help would be greatly
appreciated. Thank you.
Update:
I'm using this on a legacy application that's running on refinerycms 2.1.0.dev
Relevant code:
https://github.com/DynamoMTL/g-refinerycms/blob/244d4a89aef4ad31aed9c50a0ca4c8a2ffd3d1ac/pages/app/models/refinery/page_part.rb#L10
https://github.com/DynamoMTL/g-refinerycms/blob/244d4a89aef4ad31aed9c50a0ca4c8a2ffd3d1ac/pages/app/models/refinery/page.rb#L45-L49
https://github.com/DynamoMTL/g-refinerycms/blob/244d4a89aef4ad31aed9c50a0ca4c8a2ffd3d1ac/pages/app/models/refinery/page.rb#L11-L14
Solution
def create
#page = Refinery::Page.new(params[:page])
if #page.save!
#page.translations.create(slug: #page.slug,
title: params[:page][:title],
locale: params[:switch_locale])
flash.notice = t(
'refinery.crudify.created',
what: "'#{#page.title}'"
)
redirect_to admin_pages_path
else
render :new
end
end
Related
I try to save my variable in an action to call it from another action in my controller. I use session to save it like below:
def upload_with_dropzone
if params[:document]
#resume = #p.documents.new(document_params)
if verify_recaptcha(model: #resume)
#resume.save
session[:resume_id] = #resume.id
redirect_to prospect_upload_path
else
flash[:error] = "Cannot upload image"
redirect_to prospect_upload_path
end
end
In other action I call session[:resume_id] but it return nil
def upload_resume
if session[:resume_id].present?
#resume = Document.find(session[:resume_id])
elsif params[:interaction] && (1..5).include?(params[:interaction][:interaction_rating].to_i)
#resume = #p.documents.last
#itr.update(interaction_feedback_params)
else
#resume = #p.documents.new
end
end
Below is my routes.rb:
match "/upload" => "upload#upload_resume", via: [:get, :post]
post "/upload_resume" => "upload#upload_via_dropzone"
Something was wrong, please give me an idea!
I put byebug after session[:resume_id] = #resume.id line, and I debug the session[:resume_id]
I am trying to create an app with motorSports controller but I have an issue with routing. It looks like the name controller is not initialized. In my routes.rb I have the following:
Rails.application.routes.draw do
get "/motorsports", to: 'motorsports#index'
get "/motorsports/new", to: 'motorsports#new'
get "/motorsports/:id", to: 'motorsports#show'
post "/motorsports", to: 'motorsports#create'
get "/motorsports/:id/edit", to: 'motorsports#edit'
patch "/motorsports/:id", to: 'motorsports#update'
delete "/motorsports/:id", to: 'motorsports#destroy'
end
and in my controller I have:
class MotorSportsController < ApplicationController
def index
#motorsports = MotorSport.all
render "index.html.erb"
end
def show
#motorsport = MotorSport.find_by(id: params[:id])
end
def new
end
def create
motorsport = MotorSport.new({
model: params[:model],
make: params[:make],
image: params[:image],
description: params[:description],
cost: params[:cost],
speed: params[:speed],
driver_name: params[:driver_name]
})
motorsport.save
flash[:success] = "Motorsport Created"
redirect_to "/motorsports/#{motorsport.id}"
end
def edit
#motorsport = MotorSport.find_by(id: params[:id])
end
def update
motorsport = MotorSport.find_by(id: params[:id])
motorsport.assign_attributes({
model: params[:model],
image: params[:image],
make: params[:make],
description: params[:description],
cost: params[:cost],
speed: params[:speed],
driver_name: params[:driver_name]
})
motorsport.save
flash[:success] = "Item Updated"
redirect_to "/motorsports"
end
def destroy
#motorsport = MotorSport.find_by(id: params[:id])
#motorsport.destroy
flash[:success] = "Motorsport Deleted!"
redirect_to "/motorsports"
end
end
What wrong with my app?
Since you have MotorSportsController the routes must be like this, notice the underscore in the controller.
get "/motorsports", to: 'motor_sports#index'
Remove all routes of MotorSports and add this line.
resources motorsports
I have a rails form which takes the rails model FlsCenter and orders them alphabetically to be displayed in a drop down. When none of the fields of the form are filled out, and the form is submitted, the page gets a 500 Server Error in which the following Rails error is displayed:
undefined method `map' for nil:NilClass
Where the map function is in the following view code:
= f.select(:fls_center, #fls_centers.map{|p| [p.name, p.id]}, prompt: "Select a Program Center", selected:#school_application.fls_center)
Where #fls_centers is defined in the new method of my SchoolApplicationController here:
#fls_centers = FlsCenter.order('name ASC')
Here are the relevant controller methods from SchoolApplicationsController
def create
#school_application = SchoolApplication.new(school_application_params)
if #school_application.save_with_payment
redirect_to pay_path(id: #school_application.id)
else
#school_application.errors.full_messages.each do |msg|
flash.now[:error] = msg
end
render action: "new"
end
end
And here is my new method:
def new
if application_params
#school_application = SchoolApplication.new(application_params)
else
#school_application = SchoolApplication.new()
end
Rails.logger.debug(#fls_centers)
#fls_centers = FlsCenter.order('name ASC')
end
The only thing I can imagine that is going wrong is that render action: "new" does not execute the variable instantiation inside the new method. I do not know how to amerliorate this situation. Thank you
You are executing Rails.logger.debug(#fls_centers) before defining the #fls_centers, So make changes like shown below:
def new
#fls_centers = FlsCenter.order('name ASC')
if application_params
#school_application = SchoolApplication.new(application_params)
else
#school_application = SchoolApplication.new()
end
Rails.logger.debug(#fls_centers)
end
Hope it helps!
You guessed right. The actual new method does not get executed when you call
render action: "new"
You will have to call the variable instantiations all over again:
def create
#school_application = SchoolApplication.new(school_application_params)
if #school_application.save_with_payment
redirect_to pay_path(id: #school_application.id)
else
#school_application.errors.full_messages.each do |msg|
flash.now[:error] = msg
end
#fls_centers = FlsCenter.order('name ASC')
render action: "new"
end
end
If you have a lot of them it might be better to refactor them in a method and call that method in the new method as well as before the render call.
Newly added description: (sorry for not mentioning)
The ApplicationController.current_account is defined as:
class ApplicationController < ActionController::Base
class << self
def current_account
#current_account
end
def current_account=(value)
#current_account = value
end
end
=========
I encountered a strange performance in my current project, which is about session. The strange part is it was normal in Safari but failed in other browsers (includes chrome, firefox and opera).
There is a registration form for input of part of the key information (email, password, etc) and is submitted to an action called "create"
This is the basic code of create action:
#account = Account.new(params[:account])
if #account.save
ApplicationController.current_account = #account
session[:current_account] = ApplicationController.current_account
session[:account] = ApplicationController.current_account.id
email = #account.email
Mailer.deliver_account_confirmation(email)
flash[:type] = "success"
flash[:notice] = "Successfully Created Account"
redirect_to :controller => "accounts", :action => "create_step_2"
else
flash[:type] = "error"
flash[:title] = "Oops, something wasn't right."
flash[:notice] = "Mistakes are marked below in red. Please fix them and resubmit the form. Thanks."
render :action => "new"
end
Also I created a before_filter in the application controller, which has the following code:
ApplicationController.current_account = Account.find_by_id(session[:current_account].id) unless session[:current_account].blank?
For Safari, there is no any problem. But for the other browsers, the session[:current_account] does not exist and so produced the following error message:
RuntimeError in AccountsController#create_step_2
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Please could anyone help me?
1] don't write
ApplicationController.current_account
Just
current_account
2] in your application_controller
def current_account
session[:current_account]
end
3]
ApplicationController.current_account = #account
session[:current_account] = ApplicationController.current_account
session[:account] = ApplicationController.current_account.id
should be
session[:current_account] = #account
session[:account] = #account.id
(Rails newbie)
Hello!
I am feeling like I am reusing a lot of my code and I feel there has to be a better way to do this... (I am sure there is...)
What I have is a Settings page, where you can create categories and procedures (which belong to a category).
index Settings action:
def categories_and_procedures
#prefs = #current_practice.preferences
#category = #current_practice.categories.build
#categories = #current_practice.categories.all
#procedure = #current_practice.procedures.build
end
In the view is a list with all the current categories and a form to create a new one. In the Category model is a validation (validates_uniqueness_of :name).
The create action:
def create_category
#category = #current_practice.categories.build(params[:category])
if #category.save
flash[:notice] = "New category created: <i>#{#category.name}</i>"
redirect_to :action => "categories_and_procedures"
else
##Duplicate code!!!!!!
#prefs = #current_practice.preferences
#category = #current_practice.categories.build
#categories = #current_practice.categories.all
#procedure = #current_practice.procedures.build
##Duplicate code!!!!!!
render :action => "categories_and_procedures"
end
end
Is there a way I can move it to a function I can call? Helper? Filters?
I don't know.
Thank you!
Just write:
def create_category
#category = #current_practice.categories.build(params[:category])
if #category.save
flash[:notice] = "New category created: <i>#{#category.name}</i>"
redirect_to :action => "categories_and_procedures"
else
categories_and_procedures
render :action => "categories_and_procedures"
end
end
It will look better if you will add some setup method:
def setup_object
#prefs = #current_practice.preferences
#category = #current_practice.categories.build
#categories = #current_practice.categories.all
#procedure = #current_practice.procedures.build
end
and call it from categories_and_procedures and from create_category.