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
Related
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
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 %>
I get an error of 'Name can't be blank' and "Name only allows letters, numbers and '-'" when trying to update a name. This was working fine but I have just gone through it again while writing a test for it and realise it isn't working anymore, I can't figure out why or what has changed to break it.
Categories controller
def update
if #category.update category_params
redirect_to new_guide_category_item_path(#guide, #category)
flash[:info] = "Updated successfully"
else
render 'edit'
end
end
private
def category_params
params.require(:category).permit(:name, :template)
end
edit.html.erb
<%= render 'shared/error_messages', object: f.object %>
.....
<%= form_for([#guide, #category], url: guide_category_path) do |f| %>
<%= f.label :name, "Category name" %>
<%= f.text_field :name %>
<%= f.label :template, "Template" %>
<%= f.text_area :template, { :id => 'edit' } %>
<%= f.submit "Save", :value => "Save Template" %>
<% end %>
model
validates :name, presence: true, length: { maximum: 255 }, uniqueness: { scope: :guide_id, case_sensitive: false },
exclusion: { in: %w( guide guides category categories item items page pages post posts tag tags key keys item key item keys item-key item-keys item_key item_keys mod moderator mods moderators admin admins), message: "%{value} cant be taken." },
format: { with: /\A[a-zA-Z0-9 -]+\z/, message: "only allows letters, numbers, spaces and '-'" }
routes
resources :guides do
resources :categories, only: [:new, :create, :edit, :update] do
end
end
Anyone have an idea of what might be going wrong?
Edit added log output
Started PATCH "/guides/ghj/categories/ijijij" for ::1 at 2016-02-11 14:08:04 +1100
Processing by CategoriesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"IqiUJ8wF8ZY4t8f91+t4aOMc9xenx/F2beKjKs9GY7JzLD6ZgPYDY1ueC2s+OIDL+PjROVtMe2+GvgYdan1CDQ==", "category"=>{"name"=>"ijijijddd", "template"=>""}, "commit"=>"Save Template", "guide_id"=>"ghj", "id"=>"ijijij"}
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
[1m[36mGuide Load (0.1ms)[0m [1mSELECT "guides".* FROM "guides" WHERE "guides"."slug" = ? ORDER BY "guides"."id" ASC LIMIT 1[0m [["slug", "ghj"]]
[1m[35mCategory Load (0.1ms)[0m SELECT "categories".* FROM "categories" WHERE "categories"."slug" = ? ORDER BY "categories"."id" ASC LIMIT 1 [["slug", "ijijij"]]
[1m[36m (0.1ms)[0m [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ?[0m [["category_id", 6]]
[1m[35m (0.0ms)[0m SELECT "check_category_item_keys"."name" FROM "check_category_item_keys" WHERE "check_category_item_keys"."category_id" = ? [["category_id", 6]]
[1m[36m (0.1ms)[0m [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ?[0m [["category_id", 6], ["key_type", 1]]
[1m[35m (0.0ms)[0m SELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ? [["category_id", 6], ["key_type", 2]]
[1m[36m (0.1ms)[0m [1mSELECT "category_item_keys"."name" FROM "category_item_keys" WHERE "category_item_keys"."category_id" = ? AND "category_item_keys"."key_type" = ?[0m [["category_id", 6], ["key_type", 3]]
[1m[35mGameModsRelationship Exists (0.1ms)[0m SELECT 1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 3 LIMIT 1 [["user_id", 1]]
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
[1m[35mCategoryItemKey Exists (0.1ms)[0m SELECT 1 AS one FROM "category_item_keys" WHERE ("category_item_keys"."name" IS NULL AND "category_item_keys"."guide_id" IS NULL) LIMIT 1
[1m[36mCategory Exists (0.1ms)[0m [1mSELECT 1 AS one FROM "categories" WHERE (LOWER("categories"."name") = LOWER('ijijijddd') AND "categories"."id" != 6 AND "categories"."guide_id" = 3) LIMIT 1[0m
[1m[35m (0.0ms)[0m rollback transaction
[1m[36mCACHE (0.0ms)[0m [1mSELECT 1 AS one FROM "game_mods_relationships" WHERE "game_mods_relationships"."user_id" = ? AND "game_mods_relationships"."category_id" = 3 LIMIT 1[0m [["user_id", 1]]
Rendered shared/_error_messages.html.erb (0.5ms)
Rendered categories/edit.html.erb within layouts/application (5.6ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (0.7ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 157ms (Views: 145.1ms | ActiveRecord: 1.1ms)`
You need to add error_messages helper tag in the partial. That handles all error messages and show it properly.
It looks like your database has an existing record with the same name. Try inputting a unique name in. In your model, it is checking for :uniqueness.
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)
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.