Rails 4.0.2 ActionController::ParameterMissing in AuctionsController#create param not found: - ruby-on-rails

I am creating an auction website and I get an error when I run a command to create a new auction.
The model is simple:
class Auction < ActiveRecord::Base
has_many :bets
has_many :profiles, :through => :bets
and etc.
The controller: `
class AuctionsController < ApplicationController
def new
#auction = Auction.new
end
def create
#auction = Auction.new(auction_params)
if #auction.save
redirect_to(:action => 'index')
else
render('new')
end
end
private
def auction_params
params.require(:auction_email).permit(:auction_description, :auction_location, :auction_deadline, :auction_title, bets_attributes: [ :bet_size], )
end
end
The new.html.erb has form in this way:
<%= form_for(:auction, :url => {:action => 'create'}) do |f| %>
<%= f.text_field(:auction_title) %>
and so on for every entry.
The error message is:
ActionController::ParameterMissing in AuctionsController#create param not found: auction_email
I have been trying to solve it with "if params[:status]", but it returns an empty form and database remains empty.
Rails version 4.0.2

Use this
def auction_params
params.require(:auction).permit(:auction_email,:auction_description, :auction_location, :auction_deadline, :auction_title, bets_attributes: [ :bet_size], )
end
Assuming that Auction is your model.
params.require argument should be :auction(:modelname) and in permit method you pass the atrributes like :auction_email(:attributename) that you would like to insert/update in database.

Related

Ruby on Rails - Pass ID from another controller

I have two models. First is Taxirecord and second is Carpark. Each Taxirecord may have its own Carpark. I have a problem with passing taxirecord_id to Carpark record. I have route
car_new GET /taxidetail/:taxirecord_id/carpark/new(.:format) carparks#new
And i want to pass :taxirecord_id, which is id of taxirecord that im editing, to my create controller.
My carpark model:
class Carpark < ActiveRecord::Base
belongs_to :taxirecord
end
In controller im finding taxirecord_id by find function based on param :taxirecord_id, but id is nil when create is called. Can you please help me to find out what Im doing wrong and how Can I solve this problem? Thanks for any help!
My carpark controller
class CarparksController < ApplicationController
def new
#car = Carpark.new
end
def create
#car = Carpark.new(carpark_params, taxirecord_id: Taxirecord.find(params[:taxirecord_id]))
if #car.save
flash[:notice] = "Zaznam byl ulozen"
redirect_to root_path
else
flash[:notice] = "Zaznam nebyl ulozen"
render 'new'
end
end
private def carpark_params
params.require(:carpark).permit(:car_brand, :car_type, :driver_name, :driver_tel)
end
end
I finally get it work
Ive added <%=link_to 'New Carpark', {:controller => "carparks", :action => "new", :taxirecord_id => #taxi_record.id }%>
to my taxirecord form and to carpark form <%= hidden_field_tag :taxirecord_id, params[:taxirecord_id] %>
And to my carpark controller : #carpark.taxirecord_id = params[:taxirecord_id]
Thanks everyone for great support and help!
I'd lean towards using something like:
before_action :assign_taxirecord
...
private
def assign_taxirecord
#taxirecord = TaxiRecord.find(params[:taxirecord_id])
end
And then in the create action:
def create
#car = #taxirecord.build_carpark(carpark_params)
...
end
Obviously there's a little tailoring needed to your requirements (i.e. for what actions the before_action is called), but I hope that helps!
No need to send taxirecord id.
class Carpark < ApplicationRecord
belongs_to :taxirecord
end
class Taxirecord < ApplicationRecord
has_one :carpark
end
Rails.application.routes.draw do
resources :taxirecords do
resources :carparks
end
end
for new taxirecord
t = Taxirecord.new(:registration => "efgh", :description =>"test")
for new carpark
t.create_carpark(:description=>"abcd")
#=> #<Carpark id: 2, taxirecord_id: 2, description: "abcd", created_at: "2017-10-12 10:55:38", updated_at: "2017-10-12 10:55:38">

How to pass controller parameters in Ruby on Rails

When I write a message and when pressing the send option,
I want to store student_id, coach_id and message to the database. student_id and coach_id are being saved, but the message field is not being saved. It shows null in the database. How do I fix this?
Any help is appreciated.
Controller file:
class CourseQueriesController <ApplicationController
def index
#course_query = CourseQuery.new
end
def create
# #course_query = CourseQuery.new(course_query_params)
#course_query = CourseQuery.where(student_id: current_student.id, coach_id: "2", message: params[:message]).first_or_create
if #course_query.save
redirect_to course_queries_path, notice: 'Query was successfully send.'
else
render :new
end
end
private
def set_course_query
#course_query = CourseQuery.find(params[:id])
end
# def course_query_params
# params[:course_query].permit(:message)
# end
end
model/course_query.rb:
class CourseQuery < ActiveRecord::Base
belongs_to :student
belongs_to :coach
end
view/course_query/index.html.erb:
<%= simple_form_for (#course_query) do |f| %>
<%= f.button :submit , "Send or press enter"%>
<%= f.input :message %>
<% end %>
database /course_queries:
It seems you didn't permit :course_query.
Try to permit your params the following way:
def course_query_params
params.require(:course_query).permit(:message)
end
But according to the 2nd way you pass params (params[:message]) I think you have a bit different params structure. So try another one:
def course_query_params
params.permit(:message)
end
When you look into the params generated in the log, you will see that the message inside the course_query hash, so params[:message] should be params[:course_query][:message]
#course_query = CourseQuery.where(student_id: current_student.id, coach_id: "2", message: params[:course_query][:message]).first_or_create

form submit in rails not working, possible routing/path error unsure why?

I am trying to submit a form in rails that is just a pdf uplaod (using paperclip). There is something wrong with either my form, controller or model and i am not sure which.
this is my form:
<%= form_for #yearguide, :html => { :multipart => true } do |form| %>
<%= form.file_field :pdf %>
<%= form.submit "Add Event", class: "btn btn-primary" %>
<% end %>
my controller:
class YearController < ApplicationController
def new
#yearguide = Year.create(year_params)
end
def create
if #yearguide = Year.save
redirect_to '/'
else
render 'new'
end
end
my model:
class YearlyGuide < ActiveRecord::Base
has_attached_file :pdf
validates_attachment :document, content_type: { content_type: "application/pdf" }
end
my routes:
resources :year
I add the file and press upload, but I am being redirected to 'update.html.erb'.
The file doesn;t exist in the db, just an empty record.
When i debug the params on pressing uplaod I get this output
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"G7ZrXEiip/gsqhDObcfYhTT9kerYZGk+Zl29kWA5jos=", "year"=>{"pdf"=>#<ActionDispatch::Http::UploadedFile:0x000001029b0340 #tempfile=#<Tempfile:/var/folders/ns/ry6z7jfd6qg6j8xr2q6dw0yc0000gn/T/RackMultipart20140609-21455-1eg1sk3>, #original_filename="Artsmill Hebden Bridge Exhibition Programme 2014.pdf", #content_type="application/pdf", #headers="Content-Disposition: form-data; name=\"year[pdf]\"; filename=\"Artsmill Hebden Bridge Exhibition Programme 2014.pdf\"\r\nContent-Type: application/pdf\r\n">}, "commit"=>"Add Event", "action"=>"update", "controller"=>"year", "id"=>"9"}
=========================
EDIT
OK, so discrepancies with my naming led to the previosu errors, i started again, generating:
rails g model YearlyGuide pdf:attachment start:datetime end:datetime
rails g controller YearlyGuide new index show
now in my routes i have added
resources :yearly_guides
when i visit
/yearly_guides/new
I get this error
uninitialized constant YearlyGuidesController
I am really at a loss as to what I am doing wrong, I have done this before and never had these issues.
#iceman, thanks for your help and patience thus far.
The controller is not doing what it's supposed to do. This is the bare bones basic scheme of creating a new object in Rails.
class YearsController < ApplicationController
def new
#yearguide = Year.new
end
def create
#yearguide = Year.create(year_params)
if #yearguide.save
redirect_to '/' # I would consider redirect_to #yearguide to show the newly created object
else
render 'new'
end
end
end
EDIT:
You have to update your routes.rb to
resources :years
Since you are creating the yearguide object, rails infer that you have to do put/patch request, so request is going to update since rails getting id.
You have two options.
1) Change new method of controller like below
class YearController < ApplicationController
def new
#yearguide = Year.new
end
end
2) Override the method parameter by passing method params as 'post' inside your form tag

The action 'create' could not be found for WatchedWatchersController

I've searched high and low for this and the other solutions don't seem to help.
The error:
The action 'create' could not be found for WatchedWatchersController
I get this after clicking a link made with this code in a view:
<%= link_to 'Watchlist', { :controller => "watched_watchers",
:action => "create",
:watcher_id => current_user.id,
:watched_id => user.id},
:method => "post" %>
My model for this class is:
class Watched_Watcher < ActiveRecord::Base
attr_accessible :watched_id, :watcher_id
validates :watched_id, :watcher_id, presence: true
end
And my controller is:
class WatchedWatchersController < ApplicationController
def new
end
def create
#ww = Watched_Watcher.new
#ww.watcher_id = params[:watcher_id]
#ww.watched_id = params[:watched_id]
#ww.save
end
def update
end
def edit
end
def destroy
end
def index
end
def show
end
end
I've set up my routes as a RESTful resource:
resources :watched_watchers
And rake routes reveals:
POST /watched_watchers(.:format) watched_watchers#create
So I'm stumped and any help is appreciated. Thanks in advance.
Vimsha's comment solved it
"Rename your model to WatchedWatcher(no underscore)". And then once I fixed the model name I just needed to have a redirect back to the same page.

has_many build method, Rails

Another newbie question.
The goal: each ingredient can have zero or more unit conversions tied to it. I want to put a link to creating a new unit conversion on the page that shows a specific ingredient. I can't quite get it to work.
Ingredient Model:
class Ingredient < ActiveRecord::Base
belongs_to :unit
has_many :unit_conversion
end
Unit Conversion Model:
class UnitConversion < ActiveRecord::Base
belongs_to :Ingredient
end
Unit Conversion Controller (for new)
def new
#ingredient = Ingredient.all
#unit_conversion = #ingredient.unit_conversions.build(params[:unit_conversion])
if #unit_conversion.save then
redirect_to ingredient_unit_conversion_url(#ingredient, #comment)
else
render :action => "new"
end
end
Relevant Routes:
map.resources :ingredients, :has_many => :unit_conversions
Show Ingredient Link:
<%= link_to 'Add Unit Conversion', new_ingredient_unit_conversion_path(#ingredient) %>
This is the error:
NoMethodError in Unit conversionsController#new
undefined method `unit_conversions' for #<Array:0x3fdf920>
RAILS_ROOT: C:/Users/joan/dh
Application Trace | Framework Trace | Full Trace
C:/Users/joan/dh/app/controllers/unit_conversions_controller.rb:14:in `new'
Help! I'm all mixed up about this.
Unit Conversion Controller for new and create should be:
def new
#ingredient = Ingredient.find(params[:ingredient_id])
#unit_conversion = #ingredient.unit_conversions.build
end
def create
#ingredient = Ingredient.find(params[:ingredient_id])
#unit_conversion = #ingredient.unit_conversions.build(params[:unit_conversion])
if #unit_conversion.save
flash[:notice] = "Successfully created unit conversion."
redirect_to ingredient_unit_conversions_url(#ingredient)
else
render :action => 'new'
end
end
Also, this screencast is a nice resource for nested resources.
has_many :unit_conversion
Should be pluralized since you're calling it with
#unit_conversion = #ingredient.unit_conversions.build
your controller
def new
#ingredient = Ingredient.all
should be calling #new to setup a new Ingredient or #find to grab an existing Ingredient.
#ingredient = Ingredient.new # returns a new Ingredient
or
#ingredient = Ingredient.find(...) # returns an existing Ingredient
Which one you choose is up to your requirements.
Also, this is a typo, right?
belongs_to :Ingredient
You might want to lowercase :ingredient

Resources