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
Related
Well im going to clarify im doing it
class DuplicatesController < ApplicationController
before_action :set_venue, only: [:new]
def new
end
def create
if #venue.duplicate(venue_params)
flash[:success] = t('controller.create.success',
resource: Venue.model_name.human)
redirect_to admin_venue_url #venue
else
flash[:warning] = #venue.errors.full_messages.to_sentence
render :new
end
end
private
def set_venue
#venue = Venue.friendly.find params[:venue_id]
end
end
def venue_params
params.require(:venue).permit(:name,
:address,
:address_complement,
:city,
:phone)
end
end
def duplicate
(name,
address,
address_complement,
city,
phone)
new_venue = self.dup
new_venue.update_attributes(description: self.description,
logo: self.logo,
opening_time: self.opening_time,
closing_time: self.closing_time,
ally_id: self.ally_id)
new_venue.save
end
How can I call those params in my duplicates controller, thanks
I need to set the attributes, after create a dup because I want to save a new record with new information, but i dont know to do it in my method, someone could explain me
Thanks.
Probably the best way to do it is to pass only id/slug of original model.
Then your duplicates_controller.rb can look similar to this:
class DuplicatesController < ApplicationController
def create
old_venue = Venue.friendly.find(params[:id])
venue = old_venue.dup
venue.attributes = venue_params
if venue.save
# success render
else
# error render
end
end
private
def venue_params
params.require(:venue).permit(:permitted_attributes) # id should not be there
end
end
Of course you can refactor it, but I do not think it is needed in this situation.
Or my favourite is to change VenueController#create to something like this to allow creating from another instance:
if copy_from_id = params[:copy_from_id]
#copy_source = Venue.find_by(id: copy_from_id)
#venue = #copy_source.dup
#venue.attributes = venue_params
else
#venue = Venue.new
end
if #resource.save
...
else
...
end
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 created a polymorphic relation in a book reviewing app that I am writing. My app has the nested models: Piece >> Section >> Subsection >> Subsubsection, and I have created a model which belongs to all of these called KeyConcept, my intention being that each of these can have a key concept.
I am getting an error that I don't understand when trying to display the index action of the keyconcepts controller.
I think it might be due to a naming conflict but I don't have enough experience to understand it.
the error I am getting looks like this:
Unable to autoload constant KeyConceptsController, expected /home/david/Documents/Learning/StuddyBuddy/app/controllers/key_concepts_controller.rb to define it
else
require_or_load(expanded, qualified_name)
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
return from_mod.const_get(const_name)
end
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
My keyconcepts controller looks like this:
key_concepts_controller.rb
class Key_conceptsController < ApplicationController
include KeyconceptsHelper
def whereami
if params[:piece_id]
#piece = Piece.find(params[:piece_id])
#keyconcept = #piece.key_concepts.find(params[:id])
here = #piece
parameter = :piece_id
type = "Piece"
elsif params[:section_id]
#section = Section.find(params[:section_id])
#piece = #section.piece_id
#keyconcept = #section.key_concepts.find(params[:id])
here = #section
parameter = :section_id
type = "Section"
elsif params[:subsection_id]
#subsection = Subsection.find(params[:subsection_id])
#section = #subsection.section_id
#piece = Section.find(id=#section).piece_id
here = #subsection
parameter = :subsection_id
type = "Subsection"
elsif params[:subsubsection_id]
#subsubsection = Subsubsection.find(params[:subsubsection_id])
#subsection = #subsubsection.subsection_id
#section = Subsection.find(id=#subsection).section_id
#piece = Section.find(id=#section).piece_id
here = #subsubsection
parameter = :subsubsection_id
type = "Subsubsection"
end
end
def redirect
if type == "Piece"
#redirect = redirect_to piece_path(#piece)
elsif type == "Section"
#redirect = redirect_to piece_section_path(#piece, #section)
elsif type == "Subsection"
#redirect = redirect_to piece_section_subsection_path(#piece, #section, #subsection)
elsif type == "Subsubsection"
#redirect = redirect_to piece_section_subsection_subsubsection_path(#piece, #section, #subsection, #subsubsection)
end
end
def index
whereami.call
#piece = Piece.find(params[:piece_id])
#keyconcept = #piece.key_concepts.find(params[:id])
#redirect = redirect.call
end
def show
whereami.call
redirect.call
end
def new
#keyconcept = KeyConcept.new
#keyconcept.conceptable_id = here.id
end
def create
whereami.call
#keyconcept = KeyConcept.new(keyconcept_params)
#keyconcept.conceptable_id = params[parameter]
#keyconcept.conceptable_type = type
#keyconcept.save
redirect.call
end
def destroy
here.destroy
redirect.call
flash.notice = "#{type} '#{here.name}' from '#{#piece.name}' deleted!"
end
def edit
whereami.call
end
def update
whereami.call
here.update(keyconcept_params)
flash.notice = "#{type} '#{here.name}' Updated!"
redirect.call
end
end
the link comes from the show action of the parent Piece model here:
<% #piece.key_concepts.each do |concept| %>
<li>
<%= link_to concept.definition, [#piece, #keyconcept] %>
<!-- we didn't use #piece_key_concept_path(#piece, #keyconcept), class: 'section_name' and it worked -->
</li>
How do I link to the keyconcepts "show" action by the way? I havent been able to so i just linked to the index action :/
So the routes.rb file looks like this:
resources :pieces do
resources :sections do
resources :subsections do
resources :subsubsections
end
end
resources :links
end
resources :pieces, :sections, :subsections, :subsubsections do
resources :connections, only: [:index, :new, :edit, :update, :destroy, :create]
resources :keyconcepts, only: [:index, :new, :edit, :update, :destroy, :create, :show]
end
key_concept.rb
class KeyConcept < ActiveRecord::Base
belongs_to :conceptable, polymorphic: true
end
piece.rb
class Piece < ActiveRecord::Base
include Connectable
include Conceptable
has_many :sections
has_many :subsections, through: :sections
has_many :links
end
in models/concerns/conceptable.rb
module Conceptable
extend ActiveSupport::Concern
included do
has_many :keyconcepts, as: :conceptable
end
end
Well the problem is naming convention
Your key_concepts_controller class name should be
KeyConceptsController < ApplicationController
Also make sure you follow proper conventions
If your model name is KeyConcept file name must be key_concept.rb
Controller name should be KeyConceptsController and file name must be key_concepts_controller.rb
Same goes with routes
resources : key_concepts
Refer this for more details
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
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