I'm getting a "Unpermitted Parameters: :event_id, :attendee_id" even though I'm whitelisting the params
Started POST "/planned_events" for 127.0.0.1 at 2014-08-22 22:08:39 +0900
Processing by PlannedEventsController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"WhVcJWjo08KWqxFViUPbSZVMYzu2UgGW9E+WRExP9Wc=", "planned_event"=>
{":event_id"=>"3", ":attendee_id"=>"1"}, "commit"=>"Attend"}
Unpermitted parameters: :event_id, :attendee_id
(0.0ms) begin transaction
SQL (0.4ms) INSERT INTO "planned_events" ("created_at", "updated_at") VALUES (?, ?)
[["created_at", Fri, 22 Aug 2014 13:08:39 UTC +00:00], ["updated_at", Fri, 22 Aug 2014
13:08:39 UTC +00:00]]
(1.8ms) commit transaction
Unpermitted parameters: :event_id, :attendee_id
Completed 500 Internal Server Error in 9ms
ActionController::UrlGenerationError (No route matches {:action=>"show",
:controller=>"events", :id=>nil} missing required keys: [:id]):
app/controllers/planned_events_controller.rb:6:in `create'
Here is controllers/planned_events_controller.rb
class PlannedEventsController < ApplicationController
def create
#planned_event = PlannedEvent.new(planned_event_params)
if #planned_event.save
redirect_to event_path(planned_event_params[:event_id])
end
end
def destroy
#planned_event = PlannedEvent.find(params[:id]).destroy
redirect_to event_path(planned_event_params[:event_id] => :id)
end
private
def planned_event_params
params.require(:planned_event).permit(:event_id, :attendee_id)
end
end
Why?
The broswer is highlighting the "redirect_to..." line with the error.
ActionController::UrlGenerationError in PlannedEventsController#create
No route matches {:action=>"show", :controller=>"events", :id=>nil} missing required keys:
[:id]
I assume the :id is nil because it wont accept the id i'm passing it (from the whitelisted params), and can't find an alternative.
Your parameters have wrong names:
{":event_id"=>"3", ":attendee_id"=>"1"}
Change them in your form, incoming parameters should be
{"event_id"=>"3", "attendee_id"=>"1"}
Also, you can change whole redirect_to to shorter version:
redirect_to #planned_event.event
its a routes error.instead of moving to create its moving to show action of controller.
remember:-
your create link should be post
your show action should be get(with id passed)
i think your view needs to be looked upon where you are clicking.check the link_to or submit button or your form_for
Related
For some reason both these URLS are routing to the same file when they shouldn't be, another thing that I noticed when typing in an invalid url such as localhost:3000/topics/inexjojvnsjg it just stays on the same page.
here is what my rails console is telling me when I try to access the url
localhost:3000/topics/index
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
Rendered topics/show.html.erb within layouts/application (0.1ms)
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" =$1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 98ms (Views: 96.5ms | ActiveRecord: 0.8ms)
here is my routes file....
Rails.application.routes.draw do
devise_for :users
get 'welcome/index'
get 'welcome/about'
# get "topics/index"
# get "topics/show"
# get "topics/new"
# get "topics/edit"
#for some reason, using resources:topics, index and show both route to show
resources :topics
root to: 'welcome#index'
post :incoming, to: 'incoming#create'
end
Here is the key info:
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
The :index url for a TopicsController is "/topics".
The :show url for a TopicsController is "/topics/:id" or "/topics/1", where the last part of the url gets associated to the params[:id]. With the url "/topics/1" the :id = 1.
So when you go to the url "/topics/index" you are going to the :show action because of the "index" part of the url. You are just setting the :id to "index" instead of a Integer :id. You can see that in the output you pasted here:
Parameters: {"id"=>"index"}
TLDR: "/topics/index" is a route the will pass the Rails router but is an invalid route, because the :id is a String "index".
This is my first app in Rails 4, but I'm not sure whether Rails 4 is the problem.
I have nested resources as follows:
resources :made_games do
resources :made_game_instances
end
When I try to save a new made_game_instance this is what's happening in the log:
Started POST "/made_games/11/made_game_instances" for 127.0.0.1 at 2013-09-10 12:03:55 -0700
Processing by MadeGameInstancesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jEN2syjftjRtf3DBnijtp7gNVUEFrI+HYTUs+HFgo5M=", "made_game_instance"=>{"new_word1"=>"bluesky"}, "commit"=>"Create Made game instance", "made_game_id"=>"11"}
MadeGame Load (122.7ms) SELECT "made_games".* FROM "made_games" WHERE "made_games"."id" = $1 LIMIT 1 [["id", "11"]]
(14.0ms) BEGIN
SQL (215.9ms) INSERT INTO "made_game_instances" ("created_at", "made_game_id", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Tue, 10 Sep 2013 19:03:55 UTC +00:00], ["made_game_id", 11], ["updated_at", Tue, 10 Sep 2013 19:03:55 UTC +00:00]]
(5.7ms) COMMIT
Redirected to http://localhost:3000/made_games/11/made_game_instances/5
Completed 302 Found in 458ms (ActiveRecord: 358.3ms)
You can see that the params hash contains the hash where the new_game_instance attribute :new_word1 is assigned the value "bluesky." What I cannot figure out is why this assignment does not appear in the SQL that is subsequently generated when the new 'made_game_instances' object is created.
Additional information
Since this is Rails 4, in order to whitelist all the parameters (at least at this stage in development), I have used permit! in the params private method at the bottom of the controllers for both made_games and made_game_instances.
The made_games controller:
class MadeGamesController < ApplicationController
def new
#made_game = MadeGame.new
end
def create
#made_game = MadeGame.new(made_game_params)
if #made_game.save
flash[:notice] = "Here you go!"
redirect_to #made_game
else
flash[:notice] = "Something about that didn't work, unfortunately."
render :action => new
end
end
def show
#made_game = MadeGame.find(params[:id])
end
private
def made_game_params
params.require(:made_game).permit!
end
end
Here is a link to the github repo: https://github.com/keb97/madlibs/tree/users_making
The form used to create a new made_game_instance is:
<%= simple_form_for [#made_game, #made_game_instance] do |f| %>
<p>
<%= f.input :new_word1, label: #made_game.word1.to_s %>
</p>
<%= f.button :submit %>
<% end %>
I should also note that there is one form for made_game, and a separate form for made_game_instance, rather than a nested form, so I do not believe this is an issue of accepts_nested_attributes_for or fields_for.
In your made_games_instance_controller.rb
this line...
#made_game_instance = #made_game.made_game_instances.build(params[:made_game_instance_params])
should actually be...
#made_game_instance = #made_game.made_game_instances.build(made_game_instance_params)
There is no params hash entry with a symbol key :made_game_instance_params
I'm getting an error on the show action when new guidelines are being added to my app. This is since I changed the show action to allow for custom routes...The new guideline is added to the database correctly...
My show action in guidelines_controller.rb is
def show
#guideline = Guideline.where(title: params[:title]).first
respond_to do |format|
format.html # show.html.erb
format.json { render json: #guideline }
end
end
model guidelines.rb is
attr_accessible :content, :hospital, :title, :user_id, :guideline_id, :specialty
show view is
<p>Title: <%= link_to #guideline.title, seeguideline_path(#guideline.title) %> </p
Error message is
console says...
Started POST "/guidelines" for 127.0.0.1 at 2013-02-22 17:07:29 +1100
Processing by GuidelinesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"bQKIkSb4Wzr46FERMbU82Q1qMzd3GrGNq6Nqmr0KNhY=", "guideline"=>{"title"=>"Stackoverflo", "specialty"=>"Dermatology", "hospital"=>"Stack Hospital", "content"=>"www.stackoverflow.com"}, "commit"=>"Create Guideline"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 11 LIMIT 1
(0.1ms) begin transaction
Guideline Exists (0.4ms) SELECT 1 AS one FROM "guidelines" WHERE (LOWER("guidelines"."hospital") = LOWER('Stack Hospital') AND "guidelines"."title" = 'Stackoverflo') LIMIT 1
SQL (65.0ms) INSERT INTO "guidelines" ("content", "created_at", "hospital", "specialty", "subtitle", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["content", "www.stackoverflow.com"], ["created_at", Fri, 22 Feb 2013 06:07:29 UTC +00:00], ["hospital", "Stack Hospital"], ["specialty", "Dermatology"], ["subtitle", nil], ["title", "Stackoverflo"], ["updated_at", Fri, 22 Feb 2013 06:07:29 UTC +00:00], ["user_id", 11]]
SOLR Request (152.5ms) [ path=#<RSolr::Client:0x007f9c79f2fc48> parameters={data: <?xml version="1.0" encoding="UTF-8"?><add><doc><field name="id">Guideline 35</field><field name="type">Guideline</field><field name="type">ActiveRecord::Base</field><field name="class_name">Guideline</field><field name="title_text">Stackoverflo</field></doc></add>, headers: {"Content-Type"=>"text/xml"}, method: post, params: {:wt=>:ruby}, query: wt=ruby, path: update, uri: http://localhost:8982/solr/update?wt=ruby, open_timeout: , read_timeout: } ]
(3.7ms) commit transaction
Redirected to http://localhost:3000/guidelines/35
SOLR Request (100.3ms) [ path=#<RSolr::Client:0x007f9c79f2fc48> parameters={data: <?xml version="1.0" encoding="UTF-8"?><commit/>, headers: {"Content-Type"=>"text/xml"}, method: post, params: {:wt=>:ruby}, query: wt=ruby, path: update, uri: http://localhost:8982/solr/update?wt=ruby, open_timeout: , read_timeout: } ]
Completed 302 Found in 485ms (ActiveRecord: 69.8ms)
Started GET "/guidelines/35" for 127.0.0.1 at 2013-02-22 17:07:30 +1100
Processing by GuidelinesController#show as HTML
Parameters: {"id"=>"35"}
Guideline Load (0.2ms) SELECT "guidelines".* FROM "guidelines" WHERE "guidelines"."id" = ? LIMIT 1 [["id", "35"]]
Guideline Load (0.2ms) SELECT "guidelines".* FROM "guidelines" WHERE "guidelines"."title" IS NULL LIMIT 1
Rendered guidelines/show.html.erb within layouts/application (18.3ms)
Completed 500 Internal Server Error in 83ms
ActionView::Template::Error (undefined method `title' for nil:NilClass):
6:
7: <div class="guideline span10">
8:
9: <p>Title: <%= link_to #guideline.title, seeguideline_path(#guideline.title) %> </p>
10: <strong> <a href="<%= #guideline.content %>", target = '_blank'>Link to guideline</a> </strong>
11: <p>Hospital Name: <%= #guideline.hospital %></p>
12:
app/views/guidelines/show.html.erb:9:in `_app_views_guidelines_show_html_erb__4234346501713687788_70155056040280'
app/controllers/guidelines_controller.rb:132:in `show'
route is
get '/:title', to: 'guidelines#show', as: :seeguideline
Are you sure your route is passing the :title param? What does your console log show for the SQL request for the query? I have a feeling your route isn't passing what you think it's passing...
Also, you're going to want to handle the case that the query comes back empty anyway. Having the app blow up probably isn't what you want.
The task model has just one field : title.
I've made a form to add a new task with one single field : title
But in the create method, we can see that title is filled by "test"
but in the query, we can see "nil" ... any ideas ?
thanks
Started POST "/tasks" for 127.0.0.1 at 2013-01-03 13:16:44 -0500
Processing by TasksController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"iWaK1QX6VCyeUCueLrRNErJEtdm/ZNxg4d3LU0vKjnY=", "task"=>{"title"
=>"test"}, "commit"=>"Add a new task "}
(0.1ms) begin transaction
SQL (0.9ms) INSERT INTO "tasks" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 03 Jan 2013 18:16:44 UTC +00:00], ["title", nil], ["updated_at", Thu, 03 Jan 2013 18:16:44 UTC +00:00]]
(0.8ms) commit transaction
Redirected to http://0.0.0.0:3000/tasks
Completed 302 Found in 8ms (ActiveRecord: 1.8ms)
here is the create method
def create
#task = Task.new(params[:post])
if #task.save
redirect_to tasks_path, :notice => "Task successfully saved"
else
render "new"
end
end
The problem is that you are fetching post instead of task
#task = Task.new(params[:task])
Make sure your attribute is accessible or you won't be able to mass-assign changes to it:
class Task < ActiveRecord::Base
attr_accessible :title
end
You should have unit tests that properly exercise your models to be sure that they can be updated as you do in the controller. Those will quickly uncover any attributes which have not been correctly flagged.
Rails 2.3 and prior were not strict about this, you could mass-assign anything, but Rails 3 will not assign these attributes unless they are specifically allowed.
Make sure
attr_accessible :title
is in your Task model (task.rb)
UPDATE:
change params[:post] to params[:task]:
#task = Task.new(params[:task])
In your tasks_controller.rb , you must have create method which will handle POST request and accept parameters which are passed though request .
def create
task = Task.new(params[:task])
task.save
end
I'm trying to create user-generated posts. I know that the posts are being created in the
db, but are not displaying. Terminal puts:
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
SQL (18.4ms) INSERT INTO "events" ("content", "created_at", "updated_at",
"user_id") VALUES (?, ?, ?, ?) [["content", "Test post."], ["created_at",
Sat, 15 Oct 2011 06:36:49 UTC +00:00], ["updated_at",
Sat, 15 Oct 2011 06:36:49 UTC +00:00], ["user_id", 1]]
Redirected to http://localhost:3000/events
Started GET "/events" for 127.0.0.1 at Sat Oct 15 00:36:49 -0600 2011
Processing by EventsController#show as HTML
Completed 404 Not Found in 1ms
ActiveRecord::RecordNotFound (Couldn't find Event without an ID):
app/controllers/events_controller.rb:22:in `show'
Saying the same thing, but my app gives me the same error:
Couldn't find Event without an ID
app/controllers/events_controller.rb:22:in `show'
Is this a problem with my Events_Controller method "show":
def show
#title = "Your Events"
#event = Event.find(params[:id])
end
Or a routine issue? I'm trying to display an index of all events created.
Thanks in advance for any help.
In EventsController on line 13 you have:
redirect_to events_path
I believe this corresponds to the sixth line in your log above ("Redirected to http://localhost:3000/events").
When you use redirect_to, however, it initiates a new GET request, and as you did not specify any parameters params is consequently empty. That's why params[:id] is nil and Event.find(params[:id]) throws the error you're seeing.
Instead of using redirect_to, are you sure you shouldn't be using render :action => :show or render :action => :index? Unlike redirect_to, render does not initiate a new request, it merely renders the specified view, but within the current context (where, in your case, #event is already defined.
For more information on render vs. redirect_to read the Rails Guide on Layouts and Rendering, section 2 in particular.