Some searches don't work - ruby-on-rails

I would like to set up three search forms but the two(name and place) of the three don't work. I don't understand why it makes them different because the codes for them are almost the same.
☆members_controller
def index
if !checklogin? then return end #
if params[:name].present?
#members = Member.where("name like ?" , "%" + params[:name] + "%")
else
#members = Member.all
end
if params[:place].present?
#members = Member.where("place like ?" , "%" + params[:place] + "%")
else
#members = Member.all
end
if params[:field].present?
#members = Member.where("field like ?" , "%" + params[:field] + "%")
else
#members = Member.all
end
respond_to do |format|
format.html # index.html.erb
format.json
end
end
☆members_index(view)
<div class= "form_index">
<%= form_tag({:action=>"index"}, {:method=>"get"}) do %>
<div class="from_field_index">
<%= label_tag 'name', '名前検索:' %>
<%= text_field_tag 'name' %>
<%= submit_tag '検索' %>
<% end %>
</div>
</div>
<div class= "form_index">
<%= form_tag({:action=>"index"}, {:method=>"get"}) do %>
<div class="from_field_index">
<%= label_tag 'place', '活動場所検索:' %>
<%= text_field_tag 'place' %>
<%= submit_tag '検索' %>
<% end %>
</div>
</div>
<div class= "form_index">
<%= form_tag({:action=>"index"}, {:method=>"get"}) do %>
<div class="form_field_index">
<%= label_tag 'field', '言語分野検索:' %>
<%= text_field_tag 'field' %>
<%= submit_tag '検索' %>
<% end %>
</div>
</div>
☆command-field search
Processing by MembersController#index as HTML
Parameters: {"utf8"=>"✓", "field"=>"rails", "commit"=>"検索"}
Member Load (0.4ms) SELECT "members".* FROM "members"
CACHE (0.0ms) SELECT "members".* FROM "members"
Member Load (0.2ms) SELECT "members".* FROM "members" WHERE (field like '%rails%')
Member Load (0.1ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
Rendered members/index.html.erb within layouts/application (3.7ms)
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
☆command-place search(bad case)
Started GET "/members?utf8=%E2%9C%93&place=%E6%A8%AA%E6%B5%9C&commit=%E6%A4%9C%E7%B4%A2" for 127.0.0.1 at 2013-12-03 13:47:20 +0900
Processing by MembersController#index as HTML
Parameters: {"utf8"=>"✓", "place"=>"横浜", "commit"=>"検索"}
Member Load (0.2ms) SELECT "members".* FROM "members"
CACHE (0.0ms) SELECT "members".* FROM "members"
Member Load (0.2ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
Rendered members/index.html.erb within layouts/application (6.5ms)
CACHE (0.0ms) SELECT "members".* FROM "members" WHERE "members"."id" = ? LIMIT 1 [["id", 17]]
Rendered layouts/_header.html.erb (1.6ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 96ms (Views: 73.5ms | ActiveRecord: 1.1ms)

I think what you need is to preserve #members:
# Rails 4
#members = Member.all
# Rails 3
#members = Member.scoped
if params[:name].present?
#members = #members.where("name like ?" , "%" + params[:name] + "%")
end
if params[:place].present?
#members = #members.where("place like ?" , "%" + params[:place] + "%")
end
if params[:field].present?
#members = #members.where("field like ?" , "%" + params[:field] + "%")
end
Note there is just one call to all. Note also that in Rails 4 you would use all, while in Rails 3 you would use scoped.

Related

Rails 5 Enum With Select Field Not Saving

I understand that this has been asked many times, but for some reason none of the other solutions worked for me. I'm still unable to save a field with an enum on update.When I pass a non-enum value, like first_name, with the same form, it updates just fine, but the enum value, gender, never saves even though it shows that it is...
View:
<%= form_for(#patient, url: patient_path(params[:id])) do |f| %>
<%= f.fields_for :role do |r| %>
<%= r.fields_for :user do |u| %>
<div class="keyvalue">
<div class="key"><p>Gender</p></div>
<div class="value">
<%= u.select :gender, User.genders.map { |key, value| [key.humanize, key] }, include_blank: '-Select A Gender-' %>
</div>
<div class="push"></div>
</div><!--keyvalue-->
<% end %>
<% end %>
<% end %>
Controller:
def update
#patient = Patient.find(params[:id])
if #patient.update(patient_params)
flash[:success] = "Patient's record has been updated."
redirect_to patient_path(params[:id])
else
flash[:error] = #patient.errors.full_messages.to_sentence
redirect_to patient_path(params[:id])
end
end
Log:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"*********", "commit"=>"Update", "patient"=>{"role_attributes"=>{"user_attributes"=>{"first_name"=>"John", "middle_name"=>"Joseph", "last_name"=>"Doe", "date_of_birth"=>"1943-12-25", "gender"=>"male", "blood_type"=>"", "ethnicity"=>"", "marital_status"=>"", "id"=>"1"}, "id"=>"1"}}, "id"=>"1"}
Patient Load (0.1ms) SELECT "patients".* FROM "patients" WHERE "patients"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.0ms) begin transaction
Role Load (0.1ms) SELECT "roles".* FROM "roles" WHERE "roles"."roleable_id" = ? AND "roles"."roleable_type" = ? LIMIT ? [["roleable_id", 1], ["roleable_type", "Patient"], ["LIMIT", 1]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = ? AND ("users"."id" != ?) LIMIT ? [["username", "dev"], ["id", 1], ["LIMIT", 1]]
SQL (0.3ms) UPDATE "users" SET "gender" = ?, "updated_at" = ? WHERE "users"."id" = ? [["gender", 0], ["updated_at", "2018-04-24 02:11:23.126843"], ["id", 1]]
(2.6ms) commit transaction
Redirected to http://localhost:3000/patients/1
Completed 302 Found in 10ms (ActiveRecord: 3.2ms)
Enum in User model:
enum gender: [:male, :female, :non_binary, :rather_not_say]
Migration:
t.integer :gender
The log shows no issue, it shows the gender field being updated, but if I go to the rails console it shows gender as nil

param is missing or the value is empty: purchase

I'm trying to pass the following inside a def create method. The create method is in the transactions controller.
#purchases = current_shop.purchases.build(purchase_params)
#purchases.save!
private
def purchase_params
params.require(:purchase).permit(:shop_id, :subscription_id, :created_at)
end
And returns this error:
ActionController::ParameterMissing in TransactionsController#create
param is missing or the value is empty: purchase
Update 1
Logs:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"aJWUUEwgtG0o52MEHSOcNETj1Ueo+4NvLBD+y5LMPHxtHnOEe1Zgs3IMADdv5y+dbl7Ecgr8LMEjIG6sws9kqg==", "payment_method_nonce"=>"bf7458d2-d3a4-02c9-2593-526174f50822"}
Cart Load (0.1ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ? [["id", 93], ["LIMIT", 1]]
Shop Load (0.3ms) SELECT "shops".* FROM "shops" WHERE "shops"."id" = ? ORDER BY "shops"."id" ASC LIMIT ? [["id", 13], ["LIMIT", 1]]
LineItem Load (0.4ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? [["cart_id", 93]]
Subscription Load (0.1ms) SELECT "subscriptions".* FROM "subscriptions" WHERE "subscriptions"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
Completed 400 Bad Request in 2899ms (ActiveRecord: 1.1ms)
ActionController::ParameterMissing (param is missing or the value is empty: purchase):
app/controllers/transactions_controller.rb:60:in purchase_params'
app/controllers/transactions_controller.rb:31:in create'
Update 2
<p id="notice"><%= notice %></p>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<div class="form-container radius-box glassy-bg small-10 small-centered medium-8 large-6 columns">
<h2 class="mbs">New Transaction</h2>
<%= form_tag transactions_path do%>
<p>Please enter your payment details:</p>
<div id="dropin"></div>
<%=submit_tag "Pay #{#cart.total_price}$", class: "button mt1" %>
<%end%>
</div>

Ruby on rails - Kaminari - Paginate array of search results

I have a search form with pagination. Both things work OK separately, but if I search and then load the next page, the search parameters are forgotten and kaminari shows results regardless of what search options I selected.
I have read through the docs, and other stackoverflow posts but I'm not having any luck.
My index page has
<%= search_form_for #q, html: { id: "gig_search_form" }, remote: true do |f| %>
bla bla bla...
<% end %>
<%= link_to_next_page #gigs, 'Next Page', :remote => true %>
index.js.erb
<% if params[:page] %>
$('.gig-search-list').append("<%= j render(partial: 'gigs') %>");
<% else %>
$('.gig-search-list').html("<%= j render(partial: 'gigs') %>");
<% end %>
gigs partial:
<% #gigs.each do |gig| %>
bla bla bla...
<% end %>
Now, I have tried a few tings in my controller but I'm always gettings the same results. Originally I had;
def index
if params[:search].present?
#q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
#q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
#allgigs = #q.result(distinct: true)
#gigs = #q.result(distinct: true).page(params[:page]).per(5)
respond_to do |format|
format.js
format.html
end
end
Then;
def index
if params[:search].present?
#q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
#q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
#allgigs = #q.result(distinct: true)
#gigs = Kaminari.paginate_array(#allgigs).page(params[:page]).per(5)
respond_to do |format|
format.js
format.html
end
end
Then I tried to separate them;
def index
if params[:search].present?
#q = Gig.notexpired.where(:filled => false).near(params[:search], 500, :order => 'distance' ).ransack(params[:q])
else
#q = Gig.notexpired.where(:filled => false).ransack(params[:q])
end
#allgigs = #q.result(distinct: true)
if #allgigs.present?
unless #allgigs.kind_of?(Array)
#gigs = #allgigs.page(params[:page]).per(5)
else
#gigs = Kaminari.paginate_array(#allgigs).page(params[:page]).per(5)
end
end
respond_to do |format|
format.js
format.html
end
end
But I get the same result each time. I can see on the console that when I load more pages, the search parameters are not included.
How can I link the two things together and paginate the search results correctly?
UPDATE:
Server log after loading 'next page' using #chaitanya's answer. I have previously added search params.
Started GET "/gigs?locale=en&page=2" for 127.0.0.1 at 2016-06-29 14:45:53 +0200
Processing by GigsController#index as JS
Parameters: {"locale"=>"en", "page"=>"2"}
Gig Load (10.3ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 [["filled", "f"]]
Gig Load (1.2ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 LIMIT 5 OFFSET 5 [["filled", "f"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 7]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 2]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 8]]
Rendered gigs/_gigs.html.erb (13.4ms)
(0.5ms) SELECT DISTINCT COUNT(DISTINCT "gigs"."id") FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:53.493341') AND "gigs"."filled" = $1 [["filled", "f"]]
Rendered gigs/index.js.erb (17.2ms)
Completed 200 OK in 54ms (Views: 21.4ms | ActiveRecord: 13.0ms)
Server log when filtering with search form:
Started GET "/gigs?locale=en&utf8=%E2%9C%93&search=&q%5Bdate_gteq%5D=&q%5Bdate_lteq%5D=&q%5Bgenres_id_in%5D%5B%5D=&q%5Bsalary_cents_gteq_euros%5D=0.00&q%5Bsalary_cents_lteq_euros%5D=210.00" for 127.0.0.1 at 2016-06-29 14:45:47 +0200
Processing by GigsController#index as JS
Parameters: {"locale"=>"en", "utf8"=>"✓", "search"=>"", "q"=>{"date_gteq"=>"", "date_lteq"=>"", "genres_id_in"=>[""], "salary_cents_gteq_euros"=>"0.00", "salary_cents_lteq_euros"=>"210.00"}}
Gig Load (0.6ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) [["filled", "f"]]
Gig Load (0.5ms) SELECT DISTINCT "gigs".* FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) LIMIT 5 OFFSET 0 [["filled", "f"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 8]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 7]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 3]]
Rendered gigs/_gigs.html.erb (12.2ms)
(0.4ms) SELECT DISTINCT COUNT(DISTINCT "gigs"."id") FROM "gigs" WHERE (gigzonetime > '2016-06-29 12:45:47.973892') AND "gigs"."filled" = $1 AND ("gigs"."salary_cents" >= 0 AND "gigs"."salary_cents" <= 21000) [["filled", "f"]]
Rendered gigs/index.js.erb (15.6ms)
Completed 200 OK in 33ms (Views: 19.9ms | ActiveRecord: 5.7ms)
try following code to carry your params to next page
= link_to_next_page #gigs, "Next Page", :remote => true, :params => params
You should add the #page and #per methods to the query itself. Kaminari.paginate_array actually takes a slice from the whole array which is not very efficient.
Here's a snippet
def index
if params[:search].present?
#q = Gig.notexpired.where(:filled => false).page(params[:page]).per(5).near(params[:search], 500, :order => 'distance' ).ransack(params[:q]).results
else
#q = Gig.notexpired.where(:filled => false).page(params[:page]).per(5).ransack(params[:q]).results
end
#...
Then use the Kaminari paginate helper in your view
<%= paginate #q %>

Can't get the value stored when updating attributes

I am trying to do a marking system for moderators, in which they can select each item as "editor pick".
I've added a :editor_pick column to photos table(boolean)
and added a form in the partial for each item to have a checkbox in it:
<% if controller_name == "photos" %>
<%= simple_form_for [#user, #photo] do |f| %>
<%= f.input :editor_pick, label: "Portada" %>
<%= f.submit "Pick" %>
<% end %>
<% end %>
after that, I required the new param in strong parameters in photos controller
It's redirecting me correctly after the update, but it's not saving the new value.
When I reload the page, the boolean is still false.
My controller actions:
def by_zone
#photo = Photo.find_by(params[:id])
#user = #photo.user
render :index
end
def update
#photo = Photo.friendly.find(params[:id])
if #photo.update_attributes(photo_params)
redirect_to [current_user, #photo], notice: 'El spot se ha actualizado.'
else
render 'edit'
end
end
Edit
def photo_params
params.require(:photo).permit(:editor_pick,:url,:remote_photo_url,:thumbnail_cache ,:order,:string_tags,:tag_list, :sponsored, :photo, :terms, :title,:description,:category_id,:zone_id, :crop_x, :crop_y, :crop_w, :crop_h, sponsors_attributes: [:name, :description, :web, :facebook, :twitter, :sponsored_avatar])
end
Params Edit
Started PATCH "/users/enrique-isasi-12/photos/rustic-plastic-table" for 127.0.0.1 at 2015-01-14 16:43:55 +0100
Processing by PhotosController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wB2SmOT0zR0dwIIguRhzFdctY51LX333CzGYelxS4Hs=", "photo"=>{"editor_pick"=>"1"}, "commit"=>"Pick", "user_id"=>"enrique-isasi-12", "id"=>"rustic-plastic-table"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 ORDER BY "users"."id" ASC LIMIT 1
Notification Load (0.3ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."user_id" = $1 AND "notifications"."viewed_at" IS NULL ORDER BY "notifications"."created_at" DESC LIMIT 30 [["user_id", 5]]
Notification Load (0.2ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."user_id" = $1 AND (viewed_at IS NOT NULL) ORDER BY "notifications"."created_at" DESC LIMIT 30 [["user_id", 5]]
Photo Load (0.4ms) SELECT "photos".* FROM "photos" WHERE "photos"."slug" = 'rustic-plastic-table' ORDER BY "photos"."id" ASC LIMIT 1
(0.4ms) BEGIN
Category Load (0.3ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = $1 ORDER BY "categories"."id" ASC LIMIT 1 [["id", 2]]
Zone Load (0.3ms) SELECT "zones".* FROM "zones" WHERE "zones"."id" = $1 ORDER BY "zones"."id" ASC LIMIT 1 [["id", 6]]
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 26], ["taggable_type", "Photo"]]
SQL (0.6ms) UPDATE "photos" SET "editor_pick" = $1, "updated_at" = $2 WHERE "photos"."id" = 26 [["editor_pick", true], ["updated_at", Wed, 14 Jan 2015 15:43:55 UTC +00:00]]
false ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 26], ["taggable_type", "Photo"]]
(6.1ms) COMMIT
Redirected to http://localhost:3000/users/enrique-isasi-4/photos/rustic-plastic-table
Completed 302 Found in 31ms (ActiveRecord: 9.7ms)

Rails 4 object not saving when rendering form in another model

I have a Call model nested under Contact, a Contact has many calls. In the Edit#Contact view, I'm rendering the form to create a new call. When I submit the form, params are getting submitted, but the new Call object is not saved.
Note that if I just create a new Call from the New#Call view (form not rendered in Edit#Contact view), then the Call object is saved, so it's only a problem when rendering the form in the Edit Contact view.
console output:
Started PATCH "/contacts/54" for 127.0.0.1 at 2014-12-30 15:29:05 -0500
Processing by ContactsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wyo0S3G5NHdfFr3e2DFw/ArhA7ouKdljCP5XaHMQ5Gc=", "contact"=>{"name"=>"Sample User 3", "entity"=>"Company 3", "phone"=>"888-888-8888", "alt_phone"=>"", "dead_phone"=>"", "email"=>"sample3#example.com", "alt_email"=>"", "dead_email"=>"", "body"=>""}, "call"=>{"dial_id"=>"1", "conversation_id"=>"1", "investing_id"=>"1", "timing_id"=>"1", "motivator_id"=>"1"}, "commit"=>"Save", "id"=>"54"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", 54]]
(0.1ms) begin transaction
(0.1ms) commit transaction
Contact Store (725.1ms) {"id":54,"exception":["Elasticsearch::Transport::Transport::Errors::NotFound","[404] {\"error\":\"IndexMissingException[[contacts_development] missing]\",\"status\":404}"]}
[404] {"error":"IndexMissingException[[contacts_development] missing]","status":404}
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? AND (id < 54) ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
Property Load (0.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."contact_id" = ? [["contact_id", 54]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", "54"]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? ORDER BY updated_at DESC LIMIT 1 [["id", "54"]]
Dial Load (0.2ms) SELECT "dials".* FROM "dials"
Conversation Load (0.2ms) SELECT "conversations".* FROM "conversations"
Investing Load (0.2ms) SELECT "investings".* FROM "investings"
Timing Load (0.1ms) SELECT "timings".* FROM "timings"
Motivator Load (0.1ms) SELECT "motivators".* FROM "motivators"
Rendered calls/_form.html.haml (11.9ms)
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" ORDER BY updated_at DESC LIMIT 1
Contact Load (0.4ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at ASC LIMIT 1 [["user_id", 1]]
Rendered contacts/edit.html.haml within layouts/application (29.7ms)
Contact Load (0.5ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
CACHE (0.0ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."user_id" = ? ORDER BY updated_at DESC LIMIT 1 [["user_id", 1]]
Completed 200 OK in 1039ms (Views: 304.1ms | Searchkick: 725.1ms | ActiveRecord: 3.6ms)
Here is the updated form partial, rendered in contact#edit:
= form_for [Contact.find(params[:id]), Contact.find(params[:id]).calls.new], :url => url_for(controller: "Call", action: "create") do |f|
.form-group
= f.label :dial
= f.select :dial_id, options_from_collection_for_select(Dial.all, "id", "result"), class: 'form-control'
.form-group
= f.label :conversation
= f.select :conversation_id, options_from_collection_for_select(Conversation.all, "id", "result")
.form-group
= f.label :investing
= f.select :investing_id, options_from_collection_for_select(Investing.all, "id", "result")
.form-group
= f.label :timing
= f.select :timing_id, options_from_collection_for_select(Timing.all, "id", "result")
.form-group
= f.label :motivator
= f.select :motivator_id, options_from_collection_for_select(Motivator.all, "id", "result")
.actions
= f.submit 'Save', class: 'btn btn-success btn-sm'
Rendering the new call form in contact#edit view like this:
= render 'calls/form'
As a side question, I wasn't able to figure out the form_for path for this. So to tackle one problem at a time, I just have the very wordy path as a placeholder.
Here is the edit method in the Contacts Controller:
def edit
#contact = Contact.find(params[:id])
#user = #contact.user
#new_call = #contact.calls.build
authorize #contact
end
create method in the Call Controller:
def create
#contact = Contact.find(params[:contact_id])
#call = Call.new(call_params)
#call.contact_id = #contact.id
#call.user_id = current_user.id
if #call.save
flash[:notice] = "Your Call was successfully posted."
redirect_to edit_contact_path(#contact)
else
flash[:error] = "Your Call was not posted. Please try again."
redirect_to edit_contact_path(#contact)
end
end
routes.rb:
resources :contacts do
resources :properties
resources :calls
collection do
post :import
get :database
end
end

Resources