Rails grouped_collection_select not working when saving - ruby-on-rails

I'm using grouped_collection_select in filtering out associated information in a rails 5 form.
The first grouped_collection_select works with the Property filtering out associated data relevant to Co-operators. But, the second grouped_collection_select does work when filtering Fields associated to a Property, but comes up with an error when trying to save:
1 error prohibited this trial from being saved:
Field must exist
Form
<%= form_with(model: trial, local: true) do |f| %>
<label>Co-operator</label>
<%= f.collection_select :cooperator_id, Cooperator.order('last_name'), :id, :full_name %>
<label>Property</label>
<%= f.grouped_collection_select :property_id, Cooperator.order('last_name'), :properties, :full_name, :cooperator_id, :name %>
<label>Field</label>
<%= f.grouped_collection_select :field_id, Property.order('name'), :fields, :name, :property_id, :field_name %>
<%= f.submit 'Submit' %>
<% end %>
When I change the grouped_collection_select to a collection_select it works as should. But, this does't suit what i'm needing.
<%= f.collection_select :field_id, Field.all, :id, :field_name %>
Trials Controller
def trial_params
params.require(:trial).permit(:cooperator_id, :field_id, :property_id)
end
Trial Model
class Trial < ApplicationRecord
belongs_to :cooperator
belongs_to :property
belongs_to :field
end
Log
Processing by TrialsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"THfy+JGBYbNvzurUscPfP8LQbnnvIz1HBEfeFRiZrocXtiu4ayncEA8cNBA2IkPgcphLoa0QWsEueFBEP29OXA==", "trial"=>{"cooperator_id"=>"2", "property_id"=>"2", "field_id"=>""}, "commit"=>"Create trial", "id"=>"11"}
Cooperator Load (0.5ms) SELECT "cooperators".* FROM "cooperators" WHERE "cooperators"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/trials_controller.rb:49
Property Load (0.4ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
↳ app/controllers/trials_controller.rb:49
Field Load (0.4ms) SELECT "fields".* FROM "fields"
↳ app/views/trials/_form.html.erb:39
Rendered trials/_form.html.erb (15.3ms)
Rendered trials/edit.html.erb within layouts/application (16.6ms)
Rendered partials/_top_nav.html.erb (0.5ms)
Rendered partials/_main_nav.html.erb (0.8ms)
Completed 200 OK in 63ms (Views: 46.9ms | ActiveRecord: 8.2ms)

The form code doesn't look right to me, the first grouped collection should be something like:
<%= f.grouped_collection_select :property_id, Cooperator.order('last_name'), :properties, :full_name, :id, :name %> # Notice that the cooperator_id is replaced with id because this needs to be the value that should be set on selection. Your original code would set it to the ID of the Cooperator instead of Property.
Similarly, the second one should be something like:
<%= f.grouped_collection_select :field_id, Property.order('name'), :fields, :name, :id, :field_name %>

Related

Rails: multi-file upload produces one empty entry

I am trying to create some sort of a nested file upload. I have Images and Albums, which are related through the model AlbumImages. Images can be part of an album. The upload is carried out through Carrierwave.
I want to be able to upload Images in two ways:
directly through Images
through Albums
When uploading Images through Albums they should be uploaded and inserted into the Images and AlbumImages tables.
I can upload single images through Images, but to my surprise, my current implementation for uploading multiple images through Albums results in 2 separate data entries. (This used to work under Rails 3) The first entry is empty and the second is the one I am aiming for.
Started PATCH "/admin/albums/38" for ::1 at 2022-05-20 11:18:44 +0200
Processing by Admin::AlbumsController#update as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "album"=>{"images_attributes"=>[{"file_name"=>""}, {"file_name"=>#<ActionDispatch::Http::UploadedFile:0x00007f8f75185c70 #tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20220520-19476-kto7za.jpg>, #original_filename="some-image.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"album[images_attributes][][file_name]\"; filename=\"some-image.jpg\"\r\nContent-Type: image/jpeg\r\n">, "author"=>"Some Author", "copyright"=>"Someone", "funds_ids"=>["1", "6"]}]}, "commit"=>"Save", "id"=>"38"}
Admin Load (0.3ms) SELECT `admins`.* FROM `admins` WHERE `admins`.`id` = 1 ORDER BY `admins`.`id` ASC LIMIT 1
Album Load (0.2ms) SELECT `albums`.* FROM `albums` WHERE `albums`.`id` = 38 LIMIT 1
↳ app/controllers/admin/albums_controller.rb:39:in `update'
TRANSACTION (0.2ms) BEGIN
↳ app/controllers/admin/albums_controller.rb:40:in `update'
Fund Load (0.5ms) SELECT `funds`.* FROM `funds` WHERE `funds`.`id` IN (1, 6)
↳ app/controllers/admin/albums_controller.rb:40:in `update'
Section Load (0.3ms) SELECT `sections`.* FROM `sections` WHERE `sections`.`id` = 5 LIMIT 1
↳ app/controllers/admin/albums_controller.rb:40:in `update'
Fund Exists? (0.3ms) SELECT 1 AS one FROM `funds` WHERE `funds`.`name` = 'SomeFundA' AND `funds`.`id` != 1 LIMIT 1
↳ app/controllers/admin/albums_controller.rb:40:in `update'
Fund Exists? (0.2ms) SELECT 1 AS one FROM `funds` WHERE `funds`.`name` = 'SomeFundB' AND `funds`.`id` != 6 LIMIT 1
↳ app/controllers/admin/albums_controller.rb:40:in `update'
Album Exists? (0.2ms) SELECT 1 AS one FROM `albums` WHERE `albums`.`title` = 'SomeAlbum' AND `albums`.`id` != 38 LIMIT 1
↳ app/controllers/admin/albums_controller.rb:40:in `update'
Image Create (41.2ms) INSERT INTO `images` (`file_name`, `title`, `alt`, `author`, `copyright`, `created_at`, `updated_at`) VALUES (NULL, NULL, NULL, NULL, NULL, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
↳ app/controllers/admin/albums_controller.rb:40:in `update'
AlbumImage Create (0.2ms) INSERT INTO `album_images` (`album_id`, `image_id`, `created_at`, `updated_at`) VALUES (38, 231, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
↳ app/controllers/admin/albums_controller.rb:40:in `update'
Image Create (0.3ms) INSERT INTO `images` (`file_name`, `title`, `alt`, `author`, `copyright`, `created_at`, `updated_at`) VALUES ('some-image.jpg', NULL, NULL, 'Some Author', 'SomeCopyRight', '2022-05-20 09:18:46', '2022-05-20 09:18:46')
↳ app/controllers/admin/albums_controller.rb:40:in `update'
ImageFund Create (0.3ms) INSERT INTO `image_funds` (`image_id`, `fund_id`, `created_at`, `updated_at`) VALUES (232, 1, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
↳ app/controllers/admin/albums_controller.rb:40:in `update'
ImageFund Create (0.2ms) INSERT INTO `image_funds` (`image_id`, `fund_id`, `created_at`, `updated_at`) VALUES (232, 6, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
↳ app/controllers/admin/albums_controller.rb:40:in `update'
AlbumImage Create (0.3ms) INSERT INTO `album_images` (`album_id`, `image_id`, `created_at`, `updated_at`) VALUES (38, 232, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
↳ app/controllers/admin/albums_controller.rb:40:in `update'
TRANSACTION (3.1ms) COMMIT
↳ app/controllers/admin/albums_controller.rb:40:in `update'
Redirected to http://localhost:3000/admin/albums/38
Completed 302 Found in 2136ms (ActiveRecord: 52.4ms | Allocations: 46882)
This is is my form:
<%= form_for [:admin, #album], html: {role: 'form' } do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.fields_for :images, Image.new do |ff| %>
<div class='form-data'>
<%= ff.label :file_name, class: 'form-label' %>
<%= ff.file_field :file_name, multiple: :true, name: "album[images_attributes][][file_name]", class: 'form-field' %>
</div>
<div class="form-data">
<%= ff.label :author, class: "form-label" %>
<%= ff.text_field :author, name: "album[images_attributes][][author]", class: "form-field" %>
</div>
<div class="form-data">
<%= ff.label :copyright, class: "form-label" %>
<%= ff.text_field :copyright, name: "album[images_attributes][][copyright]", class: "form-field" %>
</div>
<div class="form-data">
<%= ff.label :fund, class: "form-label" %>
<% Fund.all.each do |fund| %>
<%= check_box_tag "album[images_attributes][][funds_ids][]", fund.id, ff.object.funds.include?(fund), class: "checkbox" %>
<%= fund.name %>
<% end %>
</div>
<% end %>
<div class='actions create'>
<%= f.submit 'Save' %>
</div>
<% end %>
Why does images_attributes get an array of two elements, instead of just one?
I hope someone can put me into the right direction. Thank you very much in advance!
In the Albums controller I set the permitted parameters for images:
private
def album_params
params.require(:album).permit(:title, :section_id, images_attributes: [:alt, :author, :copyright, :file_name, :title, funds_ids: []])
end
I was able to get it running. I needed to set include_hidden: false in the file_field
<%= image.file_field :file_name, include_hidden: false, multiple: :true, name: "album[images_attributes][][file_name]", class: 'form-field' %>
Before updating Rails and Carrierwave this used to work without this option.
TLDR; Set this in config/application.rb and it will solve all your file fields issues.
config.active_storage.multiple_file_field_include_hidden = false
# or this, but beware of load order, you can probably put this in an initializer ( untested)
ActionView::Helpers::FormHelper.multiple_file_field_include_hidden = false
On rails 7 a new configuration option has been introduced in ActionView::Helpers::FormHelper
By default it's false. and it's used here in #field_field
However, ActiveStorage in rails 7 overrides it to true by default, especially if you set config.load_defaults 7.0 because of a new feature to handle empty collections, cf.
So instead of adding include_hidden: false on ALL your file fields, especially if you have a big application, you can set this mattr_accessor either on activestorage config, or directly on the FormHelper class.

How to use a text_field to input a many_to_many association in rails?

I want to replace a select field with a text_field (to which I'm going to add autocomplete), so that all possible associations don't have to be loaded every time.
This is the form:
<%= form_for(Relation.new) do |f| %>
<%= f.hidden_field :dependency_id, :value => #article.id %>
<%= f.text_field :dependent_title %>
<%= f.submit %>
<% end %>
And this is the model:
def dependent_title
dependent.try(:title)
end
def dependent_title=(title)
self.dependent = Article.find_by_title(title) if title.present?
end
When I type in a title and press "submit" nothing happens.
It seems to work in the rails console:
irb(main):024:0> rel.dependent_title=("Optimization")
Article Load (1.0ms) SELECT "articles".* FROM "articles" WHERE "articles"."title" = ? LIMIT ? [["title", "Optimization"], ["LIMIT", 1]]
=> "Optimization"
irb(main):025:0> rel.dependent_title
=> "Optimization"
irb(main):026:0> rel.dependent_id
=> 479
I'm guessing you are looking for a field that looks like a text field but does the work of select field and with search option.
If I've understood your need correctly, you could research on select2 - a jQuery solution to your problem.
https://select2.org/
http://select2.github.io/select2/

Rails, cant get update to work with cocoon when removing attribute "undefined method '[]' for nil:NilClass"?

I am wondering if someone could tell me why I am getting this error "undefined method '[]' for nil:NilClass". This happens when I remove a picture with cocoon and try update. The method works fine for adding pictures to the edited gallery but I am getting this error when removing and updating. I tried using unless #pictures.blank? end I am assuming the problem is when cocoon removes the picture but I am not sure what to do from there. the server error is,
Started PATCH "/galleries/41" for ::1 at 2017-05-07 16:03:02 +1000
Processing by GalleriesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cG1UXCvODhYzqqAr++EAn8GvMVk7+t/eASkzDDOoPmJfw3l6ax/F2xXMhvs7FcrJ3LOuTd0sks5+2fb86kQv0Q==", "gallery"=>{"name"=>"Hellooo", "cover"=>"123456", "pictures_attributes"=>{"0"=>{"_destroy"=>"1", "id"=>"47"}, "1"=>{"_destroy"=>"1", "id"=>"48"}}}, "commit"=>"Update Gallery", "id"=>"41"}
Gallery Load (0.0ms) SELECT "galleries".* FROM "galleries" WHERE "galleries"."id" = ? LIMIT ? [["id", 41], ["LIMIT", 1]]
Unpermitted parameter: pictures_attributes
(0.0ms) begin transaction
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) commit transaction
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `[]' for nil:NilClass):
...
Perhaps if someone could explain this to me would be great!
_form.html.erb
<%= form_for(#gallery, multipart: true) do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :cover %>
<%= f.text_field :cover %>
</div>
<div id="pictures">
<%= f.fields_for :pictures do |pic| %>
<%= render 'picture_fields', f: pic %>
</div>
<% end %>
<div class="links">
<%= link_to_add_association 'add picture', f, :pictures %>
<%= f.submit %>
</div>
<% end %>
_picture_fields.html.erb
<div class="nested-fields">
<div class="field">
<%= f.label :picture %>
<%= f.file_field :picture, multiple: true, name: "pictures[picture][]" %>
<%= link_to_remove_association "remove picture", f %>
</div>
</div>
GalleriesController
def update
#gallery = Gallery.find(params[:id])
if #gallery.update(gallery_params)
params[:pictures][:picture].each do |pic|
#pictures = #gallery.pictures.create!(picture: pic)
end
flash[:success] = "Gallery Updated!"
redirect_to root_url
else
render 'edit'
end
end
Edit: Added gallery_params
def gallery_params
params.require(:gallery).permit(:id, :name, :user_id, :cover, picture_attributes: [:id, :gallery_id, :picture, :_destroy])
end
EDIT: Added create action and server log using cocoon
def create
#user = User.first
#gallery = #user.galleries.build(gallery_params)
if #gallery.save
flash[:success] = "Picture created!"
redirect_to root_url
else
render 'new'
end
end
server log
Started POST "/galleries" for ::1 at 2017-05-10 13:18:43 +1000
Processing by GalleriesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"XU3z2jMdbselPJZ2SdZdGPwiAebiPznt8GWRqmbv8LM/MIxO+sNo1z2NTaDQ3nJNm0qaBJ66ny5254MPpHZaQQ==", "gallery"=>{"name"=>"Hello", "cover"=>"123456", "pictures_attributes"=>{"1494386318553"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xac59228 #tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170510-7596-16xlrir.jpg>, #original_filename="Skateboard 1.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"gallery[pictures_attributes][1494386318553][picture]\"; filename=\"Skateboard 1.jpg\"\r\nContent-Type: image/jpeg\r\n">, "_destroy"=>"false"}, "1494386321001"=>{"picture"=>#<ActionDispatch::Http::UploadedFile:0xac59150 #tempfile=#<Tempfile:C:/Users/Lee/AppData/Local/Temp/RackMultipart20170510-7596-jxo0st.jpg>, #original_filename="Skateboard 2.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"gallery[pictures_attributes][1494386321001][picture]\"; filename=\"Skateboard 2.jpg\"\r\nContent-Type: image/jpeg\r\n">, "_destroy"=>"false"}}}, "commit"=>"Create Gallery"}
User Load (0.0ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
(0.0ms) begin transaction
User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) rollback transaction
Rendering galleries/new.html.erb within layouts/application
Rendered galleries/_picture_fields.html.erb (1.0ms)
Rendered galleries/_picture_fields.html.erb (0.5ms)
Rendered galleries/_picture_fields.html.erb (0.5ms)
Rendered galleries/_form.html.erb (42.0ms)
Rendered galleries/new.html.erb within layouts/application (58.5ms)
Completed 200 OK in 157ms (Views: 139.3ms | ActiveRecord: 0.0ms)
Mmmm. Confused. There is no params[:pictures] so that is obviously nil (check the log you posted at the top). So that is causing the error. If you are looking for the pictures posted, you should refer to params[:pictures_attributes], but not even sure what you are trying to do there: create an empty picture (again?) for each posted pictures? The pictures are saved by doing the gallery.update(gallery_params).
Note: iterating over the posted params is imho definitely wrong, because if one is deleted, it will still be posted with the nested parameter _destroy set to true, so it can be deleted correctly from the database, or if a picture already existed, it will also be posted again (and not saved, since it already exists).
[EDIT] Add short solution:
use f.fields_for :pictures in your view: this will iterate over the existing pictures a gallery has, and will allow to delete/edit existing, and add new pictures to a gallery
fix your gallery_params and allow pictures_attributes (instead of the singular form)(otherwise nothing is saved)
in your controller just write #gallery.update_attributes(gallery_params) and do not iterate over params[:pictures] at all (remove that part) and it should just work (because the update_attributes will already have done this, at least if you want to iterate manually use pictures_attributes)
It looks like your gallery_params method is not permitting your pictures_attributes. You didn't post that code but I noticed in the error log Unpermitted parameter: pictures_attributes which means that your strong parameters(the gallery_params method) is filtering those parameters out.
Basically the whole point of the strong parameters is to make sure that you only pass through keys that you actually want to get passed through. So your controller is like "Nobody told me I am supposed to accecpt picture_attributes so I won't allow them through." Then your code is expecting there to be a picture object in the pictures array, but the pictures array doesn't have the picture object causing an error.
Can you post the code for gallery params? Can you also post the contents of params? Full disclosure: I don't really know rails 5 so there could be some other stuff going on.

If URL contains string?

How can I use a conditional to do one thing if :name is passed to the _form and another thing if that :name isn't passed?
With :name passed:
Started GET "/inspirations/new?inspiration%5Bname%5D=Always+trust+yourself+more+than+you+doubt+yourself" for 127.0.0.1 at 2016-11-08 01:00:44 -0500
Processing by InspirationsController#new as HTML
Parameters: {"inspiration"=>{"name"=>"Always trust yourself more than you doubt yourself"}}
Without :name passed:
Started GET "/inspirations/new" for 127.0.0.1 at 2016-11-08 01:16:18 -0500
Processing by InspirationsController#new as */*
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Inspiration Load (0.4ms) SELECT "inspirations".* FROM "inspirations" WHERE "inspirations"."id" IS NULL LIMIT 1
Rendered inspirations/_form.html.erb (4.1ms)
Rendered inspirations/new.html.erb within layouts/modal (5.9ms)
Completed 200 OK in 49ms (Views: 41.9ms | ActiveRecord: 0.7ms)
_form
<%= simple_form_for(#inspiration) do |f| %>
<%= f.text_area :name %>
<% if params[:name].nil? %> # Should Be Triggered If No :name Is Present in URL
etc...
<% end %>
<% end %>
So for example when a URL includes a string:
http://www.livetochallenge.com/inspirations/new?inspiration%5Bname%5D=Life+would+be+easier+if+I+had+the+source+code.
But I often use a URL shortener.
Didn't work for me:
Is there a way to check if part of the URL contains a certain string
including url parameters in if statement
Try this:
<%= simple_form_for(#inspiration) do |f| %>
<%= f.text_area :name %>
<% if params[:inspiration].try(:[], :name).nil? %> # Should Be Triggered If No :name Is Present in URL
etc...
<% end %>
<% end %>
This will check :name inside params[:inspiration] only if the later is present. So, no error should occur.
You can avoid the error using fetch.
<%= simple_form_for(#inspiration) do |f| %>
<%= f.text_area :name %>
<%= f.text_area :name %>
<% name = params.fetch(:inspiration, {}).fetch(:name, nil) %>
<% if name.nil? %>
etc...
<% end %>
<% end %>

Implement correctly models for a ride share application - Rails

I have a Rails app, with 2 models : a User model and a Micropost model, from the RoR tutorial.
I implemented the whole Twitter app, to get started with a Rails.
Now I wanted to use the code I have to create a ride share application : people posting rides that they are going to make (when they drive), apply to a ride when they want to be a passenger, following users to get their rides on their feed (instead of just posts) and being followed by other users. We see that a "user" actually plays 2 roles : driver and passenger. I'm good with the driver : it's just the author of the post, no change to make.
But not passenger...
What I want : have a "Hop in" button on each ride post -- user clicks, the button turns to "Jump off" and we have a record that gets created in the db, in the PassengerRide table (ride_id, passenger_id) // when we click on "Jump off" the button turns to "Hop in" and the record that had been saved gets deleted from the PassengerRide table.
What I did : I tried to mimic the implementation of the following and adapt to what I want
1) generated the PassengerRide table (many to many relation between a User and a Micropost)
with
rails generate model PassengerRide ride_id:integer passenger_id:integer
2) in passenger_ride.rb :
attr_accessible :ride_id
EDIT: I removed this hoping it'd fix the problem and it didn't solve the problem.
3)established the associations! (remember: I only have a User table and a Micropost table)
in user.rb
has_many :passenger_rides, foreign_key: "passenger_id", dependent: :destroy
has_many :rides, through: :passenger_rides, class_name: "Micropost"
in passenger_ride.rb
belongs_to :passenger, class_name: "User"
belongs_to :ride, class_name: "Micropost"
in micropost.rb
has_many :passenger_rides, foreign_key: "ride_id", dependent: :destroy
has_many :passengers, through: :passenger_rides, source: :passenger, class_name: "User"
4) methods in the user model to create the passenger_ride records
def hopped_in?(ride)
self.passenger_rides.find_by_ride_id(ride.id)
end
def hop_in!(ride)
self.passenger_rides.create!(ride_id: ride.id)
end
def jump_off!(ride)
self.passenger_rides.find_by_ride_id(ride.id).destroy
end
5) add rides to the user controller
in routes.rb
resource :users do
member do
get :rides
end
end
6) views :
in app/views/microposts/_hopin.html.erb
<%= form_for(current_user.rides.build(ride_id: #micropost.id)) do |f| %>
<div><%= f.hidden_field :ride_id %></div>
<%= f.submit "Hop in", :class => "btn btn-large btn-primary" %>
<% end %>
in app/views/microposts/_jumpoff.html.erb
<%= form_for(current_user.rides.find_by_ride_id(#micropost),
html: { method: :delete }) do |f| %>
<%= f.submit "Jump off", :class => "btn btn-large" %>
<% end %>
in app/views/microposts/_ride_form.html.erb
<div id="ride_form">
<% if current_user.hopped_in?(#micropost) %>
<%= render 'microposts/jumpoff' %>
<% else %>
<%= render 'microposts/hopin' %>
<% end %>
</div>
The local server rendered the following :
Started GET "/" for 127.0.0.1 at 2012-03-30 21:39:06 -0400
Processing by PagesController#home as HTML
←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users
"."remember_token" = 'R8o1mYu49bhrsrIyIPO-ow' LIMIT 1←[0m
←[1m←[35m (0.0ms)←[0m SELECT DISTINCT "users".id FROM "users" INNER JOIN "rel
ationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships
"."follower_id" = 3
←[1m←[36m (1.0ms)←[0m ←[1mSELECT COUNT(*) FROM "microposts" WHERE "microposts
"."user_id" = 3←[0m
Rendered shared/_user_info.html.erb (4.0ms)
←[1m←[35m (0.0ms)←[0m SELECT COUNT(*) FROM "users" INNER JOIN "relationships"
ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."follower
_id" = 3
←[1m←[36m (0.0ms)←[0m ←[1mSELECT COUNT(*) FROM "users" INNER JOIN "relationsh
ips" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."foll
owed_id" = 3←[0m
←[1m←[35m (0.0ms)←[0m SELECT COUNT(*) FROM "microposts" INNER JOIN "passenger
_rides" ON "microposts"."id" = "passenger_rides"."ride_id" WHERE "passenger_ride
s"."passenger_id" = 3
Rendered shared/_stats.html.erb (15.0ms)
←[1m←[36m (0.0ms)←[0m ←[1mSELECT COUNT(*) FROM "microposts" WHERE (user_id IN
(1) OR user_id = 3)←[0m
←[1m←[35mMicropost Load (0.0ms)←[0m SELECT "microposts".* FROM "microposts" W
HERE (user_id IN (1) OR user_id = 3) ORDER BY microposts.created_at DESC LIMIT 3
0 OFFSET 0
←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users
"."id" = 3 LIMIT 1←[0m
←[1m←[35mPassengerRide Load (0.0ms)←[0m SELECT "passenger_rides".* FROM "pass
enger_rides" WHERE "passenger_rides"."passenger_id" = 3 AND "passenger_rides"."r
ide_id" IS NULL LIMIT 1
WARNING: Can't mass-assign protected attributes: ride_id
Rendered microposts/_hopin.html.erb (4.0ms)
Rendered microposts/_ride_form.html.erb (9.0ms)
Rendered shared/_feed_item.html.erb (15.0ms)
Rendered shared/_feed.html.erb (20.0ms)
Rendered pages/home.html.erb within layouts/application (46.0ms)
Completed 500 Internal Server Error in 155ms
ActionView::Template::Error (undefined method `ride_id' for #<Micropost:0x5f53a1
0>):
1: <%= form_for(current_user.rides.build(ride_id: #micropost.id)) do |f| %>
2: <div><%= f.hidden_field :ride_id %></div>
3: <%= f.submit "Hop in", :class => "btn btn-large btn-primary" %>
4: <% end %>
app/views/microposts/_hopin.html.erb:2:in `block in _app_views_microposts__hop
in_html_erb___661389729_52209444'
app/views/microposts/_hopin.html.erb:1:in `_app_views_microposts__hopin_html_e
rb___661389729_52209444'
app/views/microposts/_ride_form.html.erb:6:in `_app_views_microposts__ride_for
m_html_erb___54007412_52167144'
app/views/shared/_feed_item.html.erb:12:in `_app_views_shared__feed_item_html_
erb___851424678_48417588'
app/views/shared/_feed.html.erb:3:in `_app_views_shared__feed_html_erb___10631
41135_48687708'
app/views/pages/home.html.erb:19:in `_app_views_pages_home_html_erb__74940785_
48176280'
Thank you so much for taking time to read that, I couldn't be more thorough !
Shouldn't it be passenger_rides instead of rides in the _hopin.html.erb partial? I.e.:
<%= form_for(current_user.passenger_rides.build(ride_id: #micropost.id)) do |f| %>
<div><%= f.hidden_field :ride_id %></div>
<%= f.submit "Hop in", :class => "btn btn-large btn-primary" %>
<% end %>

Resources