Rails render not opening new page with searchkick - ruby-on-rails

I'm trying to make a search kick results page. However when i click search the console states i've got the correct id but the results page isnt opening.
I have this for my controller
def search
#events = Newevent.search params[:search]
render 'events/results'
end
I also have this as my search form
<div class="input-group input-group-lg">
<%= text_field_tag :search, params[:search], class: "form-control", autocomplete: "off" %>
<div class="input-group-btn">
<%= submit_tag "Search", class: "btn btn-primary" %>
</div>
</div>
<% end %>
And this is inside the results html.erb
<% #events.each do |event| %>
<h1>Test</h1>
<%= event.eventname %>
<% end %>
Heres what the console states when i start the search
Started GET "/search_events?utf8=%E2%9C%93&search=capital&commit=Search" for ::1 at 2015-11-21 15:18:33 +0000
Processing by NeweventsController#search as JS
Parameters: {"utf8"=>"✓", "search"=>"capital", "commit"=>"Search"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 2]]
Newevent Search (9.8ms) curl http://localhost:9200/newevents_development/_search?pretty -d '{"query":{"dis_max":{"queries":[{"match":{"_all":{"query":"capital","operator":"and","boost":10,"analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"capital","operator":"and","boost":10,"analyzer":"searchkick_search2"}}},{"match":{"_all":{"query":"capital","operator":"and","boost":1,"analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}},{"match":{"_all":{"query":"capital","operator":"and","boost":1,"analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true}}}]}},"size":100000,"from":0,"fields":[]}'
Newevent Load (0.5ms) SELECT "newevents".* FROM "newevents" WHERE "newevents"."id" = 20
Rendered events/results.html.erb within layouts/application (1.6ms)
Rendered layouts/_navigation.html.erb (0.1ms)
Rendered layouts/_messages.html.erb (0.1ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 93ms (Views: 79.5ms | Searchkick: 9.8ms | ActiveRecord: 1.0ms)

Base on your server's log, I think you setup your search form as a remote form (you will see you have remote: true in your form_for tag)
Processing by NeweventsController#search as JS
Do you see text: as JS in the above line. That's because you submit a remote form. That means when you submit your form, the form will call ajax to server wait for server to return a js code to run on client side.
To solve your problem, just remove remote: true in your form_for tag, and everything will be ok ;)

Related

Trouble displaying individual images from array (Rails active storage)

I'm sure this should be simple and I'm overlooking something daft.
Rails 6.1.1
Ruby 3.0.0
I'm using active storage to attach multiple 'photos' to my Articles model.
In Articles 'show.html.erb' I want to place the photos in a number of different places in the article, not looping all of them in a row in the same place.
show.html.erb
<% #first2photos.each do |article| %>
<%= image_tag(article.photos) %>
<% end %>
articles.controller.erb
def show
#first2photos = Article.order("created_at DESC").first(2)
end
This gives the following error:
Can't resolve image into URL: undefined method `to_model' for #<ActiveStorage::Attached::Many:0x00007fdbbfa852a8 #name="photos", #record=#<Article id: 1, title: "This is the first post", snippet: "Hopefully this will also attach a couple of photos", body: "Nullam ac odio eget mauris accumsan malesuada. Nul...", created_at: "2021-01-30 20:50:31.766713000 +0000", updated_at: "2021-01-30 21:04:09.424224000 +0000">> Did you mean? to_yaml
If I loop on 'photo's with:
<% #first2photos.each do |photos| %>
<%= image_tag(#article.photos) %>
<% end %>
I still get the following error:
Can't resolve image into URL: undefined method `to_model'...
Changing that to:
<% #first2photos.each do |photos| %>
<%= image_tag(photos) %>
<% end %>
The page now loads, but shows a broken image icon, with no error. Checking the log in Terminal, I'm not sure I can see where it's attempting to access the photo.
Started GET "/articles/1" for ::1 at 2021-01-31 20:56:58 +0000
Processing by ArticlesController#show as HTML
Parameters: {"id"=>"1"}
Article Load (2.9ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
↳ app/controllers/articles_controller.rb:64:in `set_article'
Article Load (1.4ms) SELECT "articles".* FROM "articles" ORDER BY created_at DESC LIMIT $1 [["LIMIT", 2]]
↳ app/controllers/articles_controller.rb:11:in `show'
Rendering layout layouts/application.html.erb
Rendering articles/show.html.erb within layouts/application
Rendered articles/show.html.erb within layouts/application (Duration: 0.4ms | Allocations: 155)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 8.6ms | Allocations: 4208)
Completed 200 OK in 20ms (Views: 9.3ms | ActiveRecord: 4.4ms | Allocations: 6022)
You need to loop over the photos collection to use image_tag for each photo.
<% #first2photos.each do |article| %>
<% article.photos.each do |photo|
<%= image_tag(photo) %>
<% end %>
<% end %>
If you want to show only one image per article then:
<% #first2photos.each do |article| %>
<%= image_tag(article.photos.first) %>
<% end %>
I would change the name first2photos to first2articles to make it less confusing too.

view does not render when using a form_with, with a url and existing controller action in ROR

When I attempt to submit a post request using form_with and a custom url pointing to an existing controller action, the view does not render.
I am trying to follow a tutorial which shows the differences between form_for and form_tag in ROR, but trying to use form_with instead because the former are going to be deprecated. It all works fine using the form_tag.
The Rails Guides for form helpers only shows form_for and form_tag.
The ROR API docs does show some examples with form_with, but I am not connecting the dots.
<!-- index.html.erb -->
<%= form_with(url: create_users_path) do |f| %>
<%= f.text_field :name, placeholder: "Your Name" %>
<%= f.number_field :age, placeholder: "Your Age" %>
<%= f.submit "Create Profile" %>
<% end %>
and
# routes.rb
get 'users' => 'welcome#users'
post 'create_users' => 'welcome#users'
and
# welcome_controller.rb
def users
if params[:name] != nil
user = User.create(name: params[:name], age: params[:age])
end
#users = User.all
end
and
<!--users.html.erb-->
<table border="2">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.age %></td>
</tr>
<% end %>
</table>
<br>
<%= link_to "Return to Index", root_path %>
My users table updates with the data sent in the form, but I am left on my index.html.erb view instead of at users.html.erb where I expect, as the .create active record call is in the users action.
And here are my server logs from the POST request where welcome/users.html.erb is said to have been rendered (browser doesn't update.) A GET will render the page in the browser.
Started POST "/create_users" for 127.0.0.1 at 2018-08-05 22:18:26 -0400
Processing by WelcomeController#users as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"upoeRgVioYXs+Z+vsyBdg4CnXrSTOMCzPFXSHUKT2IlrVMDtvnf8UD7yJkPMYw0zkwihmPvY3Yq0BlG7RFsIIQ==", "name"=>"testy testerson", "age"=>"28", "commit"=>"Create Profile"}
(0.1ms) begin transaction
SQL (2.4ms) INSERT INTO "users" ("name", "age", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "testy testerson"], ["age", 28], ["created_at", "2018-08-06 02:18:26.466212"], ["updated_at", "2018-08-06 02:18:26.466212"]]
(3.2ms) commit transaction
Rendering welcome/users.html.erb within layouts/application
User Load (0.2ms) SELECT "users".* FROM "users"
Rendered welcome/users.html.erb within layouts/application (4.3ms)
Completed 200 OK in 147ms (Views: 56.8ms | ActiveRecord: 6.7ms)
Started GET "/users" for 127.0.0.1 at 2018-08-05 22:19:19 -0400
Processing by WelcomeController#users as HTML
Rendering welcome/users.html.erb within layouts/application
User Load (0.3ms) SELECT "users".* FROM "users"
Rendered welcome/users.html.erb within layouts/application (2.3ms)
Completed 200 OK in 60ms (Views: 51.1ms | ActiveRecord: 0.3ms)
I am pretty sure that having the create and show in the same action is not a best practice...just trying to get a better understanding of rails. What am I missing about the routes/controller/views relationship?
I am using rails versrion 5.1.6

"invalid-request-cookie" error

So for some reason I'm getting a "invalid-request-cookie" when trying to submit a ReCaptcha. I'm using the ReCaptcha gem.
What am I doing wrong?
Also, I have my ReCaptcha public and private keys in my bash_profile so that shouldn't be an issue.
UsersStepsController.rb
class UserStepsController < ApplicationController
def show
end
def update
#user = current_user
captcha_message = "The data you entered for the CAPTCHA wasn't correct. Please try again"
if !verify_recaptcha(message: captcha_message)
render :add_recaptcha
else
redirect_to root_path and return
end
end
def add_recaptcha
#user = current_user
end
end
Routes.rb
patch '/user_steps', to: 'user_steps#update', as: 'update_user_steps'
resources :user_steps, except: [:update]
get '/add_recaptcha', to: 'user_steps#add_recaptcha'
user_steps/add_captcha.html.erb
<fieldset class="clearfix">
<div class="g-recaptcha" data-sitekey="6LeUQ_xxxx_etc"></div>
<div align="center"><%= recaptcha_tags :public_key => ENV['RECAPTCHA_PUBLIC_KEY'] %></div>
<%= form_for(#user, {url: update_user_steps_path(id: #user.to_param)}) do |f| %>
<br>
<%= f.submit "Done", class: "styling" %>
<% end %>
</div>
</fieldset>
Update Logs:
Started PATCH "/user_steps?id=35" for 127.0.0.1 at 2014-11-26 11:55:52 -0500
Started PATCH "/user_steps?id=35" for 127.0.0.1 at 2014-11-26 11:55:52 -0500
Processing by UserStepsController#update as HTML
Processing by UserStepsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sEOPI8KVuXLPSLjXVajF8QcWeLcUpibgHq6hHqgtwGA=", "commit"=>"Done", "id"=>"35"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sEOPI8KVuXLPSLjXVajF8QcWeLcUpibgHq6hHqgtwGA=", "commit"=>"Done", "id"=>"35"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 35 ORDER BY "users"."id" ASC LIMIT 1
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = 35 ORDER BY "users"."id" ASC LIMIT 1
Rendered layouts/_messages.html.erb (0.1ms)
Rendered layouts/_messages.html.erb (0.1ms)
Rendered layouts/headers/_header_sign_up_step2.html.erb (0.2ms)
Rendered layouts/headers/_header_sign_up_step2.html.erb (0.2ms)
Rendered user_steps/add_recaptcha.html.erb within layouts/application (28.2ms)
Rendered user_steps/add_recaptcha.html.erb within layouts/application (28.2ms)
Completed 200 OK in 919ms (Views: 800.4ms | ActiveRecord: 0.8ms)
Completed 200 OK in 919ms (Views: 800.4ms | ActiveRecord: 0.8ms)
Here is your answer :) You need to include that inside your form unless the value won't be saved!
<fieldset class="clearfix">
<%= form_for(#user, {url: update_user_steps_path(id: #user.to_param)}) do |f| %>
<div align="center"><%= recaptcha_tags :public_key => ENV['RECAPTCHA_PUBLIC_KEY'] %></div>
<br>
<%= f.submit "Done", class: "styling" %>
<% end %>
</div>
</fieldset>

Form Submit button only works after reload

I have an index page that builds a table, and I am trying to allow users to edit line's in the table. I am trying to do this in the most basic way possible - no javascript, ajax, etc, unless Rails is supplying it.
I have my table displaying fine in the index method, and there is a form as the last row in the table that can be used to add a new row. The new form works fine. Each row has an edit link that routes to the controller's edit method. The controller sets the object to be edited, and renders index, this time with a form in the row that is to be edited. My problem is that this form will not submit, but if I refresh the page it will submit.
The fact that the page will submit after a refresh is very confusing. I don't see how a refresh would do anything different then clicking the link (it should still go through the same routing, with the same variables right?) and I can't see any difference in the form html before and after the refresh. Any have ideas on what might be happening?
I am not sure what code to even start looking in, but here goes;
index.html.erb
...
<tbody>
<% #boms.each do |line| %>
<% if line == #bom %>
<%= render("form_in_table", form_objects: #bom , button_text: "Update") %>
<% else %>
<%= render("bom_in_table", line: line) %>
<% end %>
<% end %>
<% if #bom.new_record? %>
<%= render("form_in_table", form_objects: [#li, #bom] , button_text: "Add") %>
<% end %>
</tbody>
...
_form_in_table.html.erb
<%= form_for(form_objects, html: {class: "form-in-table"}) do |f| %>
<tr>
<td><%= f.text_field :quantity %></td>
<td colspan="2">
<%= f.select(:part_id,
options_from_collection_for_select(#parts, :id, :pricebook_name),
prompt: "Select a Part",) %></td>
<td></td>
<td></td>
<td></td>
<td><%= f.submit(button_text, class: "btn btn-primary btn-mini") %></td>
</tr>
<% end %>
_bom_in_table.html.erb
<tr>
<td><%= line.quantity%></td>
<td><%= line.part_number %></td>
<td><%= line.part_description %></td>
<td><%= number_to_currency(line.part_cost) %></td>
<td><%= line.part_unit %></td>
<td><%= number_to_currency(line.extension) %></td>
<td><%= link_to('Edit', edit_bom_path(line)) %></td>
</tr>
boms_controller.rb
...
def edit
#bom = Bom.find(params[:id])
#li = #bom.line_item
#boms = #li.boms.sorted_by_part_number
#parts = Part.sorted_by_number
render 'index'
end
...
In case this is useful to deciphering the code/intent, I have collections of line_items, parts, and boms; line_item has many boms, and line_item has many parts through boms. In addition to the part/line item relationship, boms have a quantity. Bom is short for bill of materials. #li is the line_item that is being manipulated. The form I having trouble with is for viewing/adding/editing the collection of boms (quantitys and parts) that belong to a line item.
ADDING LOGS
Started GET "/line_items/8/boms" for 127.0.0.1 at 2013-10-14 14:27:27 -0400
Processing by BomsController#index as HTML
Parameters: {"line_item_id"=>"8"}
[1m[35mLineItem Load (0.0ms)[0m SELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT 1 [["id", "8"]]
[1m[36mLineItemSubClass Load (1.0ms)[0m [1mSELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1[0m [["id", 8]]
[1m[35mLineItemClass Load (4.0ms)[0m SELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1 [["id", 1]]
Rendered shared/_error_messages.html.erb (3.0ms)
[1m[36mBom Load (1.0ms)[0m [1mSELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC[0m [["line_item_id", 8]]
[1m[35mPart Load (0.0ms)[0m SELECT "parts".* FROM "parts" WHERE "parts"."id" = ? ORDER BY "parts"."id" ASC LIMIT 1 [["id", 1]]
Rendered boms/_bom_in_table.html.erb (96.0ms)
[1m[36mPart Load (1.0ms)[0m [1mSELECT "parts".* FROM "parts" ORDER BY "parts".number ASC[0m
Rendered boms/_form_in_table.html.erb (103.0ms)
[1m[35m (24.0ms)[0m SELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8
Rendered boms/index.html.erb within layouts/boms (477.0ms)
Rendered layouts/_shim.html.erb (1.0ms)
Rendered layouts/_header.html.erb (0.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Rendered layouts/application.html.erb (69.0ms)
Completed 200 OK in 671ms (Views: 601.0ms | ActiveRecord: 31.0ms)
Started GET "/boms/22/edit" for 127.0.0.1 at 2013-10-14 14:28:13 -0400
Processing by BomsController#edit as HTML
Parameters: {"id"=>"22"}
[1m[36mBom Load (0.0ms)[0m [1mSELECT "boms".* FROM "boms" WHERE "boms"."id" = ? LIMIT 1[0m [["id", "22"]]
[1m[35mLineItem Load (1.0ms)[0m SELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? ORDER BY "line_items"."id" ASC LIMIT 1 [["id", 8]]
[1m[36mLineItemSubClass Load (1.0ms)[0m [1mSELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1[0m [["id", 8]]
[1m[35mLineItemClass Load (0.0ms)[0m SELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1 [["id", 1]]
Rendered shared/_error_messages.html.erb (0.0ms)
[1m[36mBom Load (1.0ms)[0m [1mSELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC[0m [["line_item_id", 8]]
[1m[35mPart Load (0.0ms)[0m SELECT "parts".* FROM "parts" ORDER BY "parts".number ASC
Rendered boms/_form_in_table.html.erb (25.0ms)
[1m[36m (0.0ms)[0m [1mSELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8[0m
Rendered boms/index.html.erb within layouts/boms (41.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Rendered layouts/application.html.erb (54.0ms)
Completed 200 OK in 113ms (Views: 104.0ms | ActiveRecord: 3.0ms)
Started GET "/boms/22/edit" for 127.0.0.1 at 2013-10-14 14:28:37 -0400
Processing by BomsController#edit as HTML
Parameters: {"id"=>"22"}
[1m[35mBom Load (0.0ms)[0m SELECT "boms".* FROM "boms" WHERE "boms"."id" = ? LIMIT 1 [["id", "22"]]
[1m[36mLineItem Load (0.0ms)[0m [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."id" = ? ORDER BY "line_items"."id" ASC LIMIT 1[0m [["id", 8]]
[1m[35mLineItemSubClass Load (0.0ms)[0m SELECT "line_item_sub_classes".* FROM "line_item_sub_classes" WHERE "line_item_sub_classes"."id" = ? ORDER BY "line_item_sub_classes"."id" ASC LIMIT 1 [["id", 8]]
[1m[36mLineItemClass Load (1.0ms)[0m [1mSELECT "line_item_classes".* FROM "line_item_classes" WHERE "line_item_classes"."id" = ? ORDER BY "line_item_classes"."id" ASC LIMIT 1[0m [["id", 1]]
Rendered shared/_error_messages.html.erb (0.0ms)
[1m[35mBom Load (1.0ms)[0m SELECT "boms".* FROM "boms" INNER JOIN "parts" ON "parts"."id" = "boms"."part_id" WHERE "boms"."line_item_id" = ? ORDER BY "parts".number ASC [["line_item_id", 8]]
[1m[36mPart Load (0.0ms)[0m [1mSELECT "parts".* FROM "parts" ORDER BY "parts".number ASC[0m
Rendered boms/_form_in_table.html.erb (5.0ms)
[1m[35m (0.0ms)[0m SELECT SUM(quantity * cost) AS sum_id FROM "parts" INNER JOIN "boms" ON "boms"."part_id" = "parts"."id" WHERE "boms"."line_item_id" = 8
Rendered boms/index.html.erb within layouts/boms (27.0ms)
Rendered layouts/_shim.html.erb (1.0ms)
Rendered layouts/_header.html.erb (8.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Rendered layouts/application.html.erb (60.0ms)
Completed 200 OK in 131ms (Views: 94.0ms | ActiveRecord: 2.0ms)
Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-transition.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-affix.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-alert.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-collapse.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-modal.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:38 -0400
Started GET "/assets/bootstrap-carousel.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-button.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-dropdown.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-scrollspy.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-tab.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-typeahead.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-tooltip.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap-popover.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/bootstrap.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/parts.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2013-10-14 14:28:39 -0400
I believe this is an HTML issue, not a Rails issue. Per this discussion Form inside a table, <form> can not be placed inside <table> or <tbody> or <tr>. After moving the <form> to wrap the table and putting the controls inside the respective <td> the form works.
I still don't understand why refreshing the page made the form work, but...
If it's Rails 4, it's probably because of Turbolinks.
Try putting
data-no-turbolink="true" inside your body tag
This may work, it happend once to me.
This type of error is most frequently one generated by invalid HTML. Various sources of errors can be:
missing < or >
HTML tag not closed
orphaned HTML closing tag (where no opening one is related); in complex forms I've had extra </div>s lying about...
Forms nested within table or tr tags (within td is allowed)
The form helpers need to be properly nested, otherwise these quirks will bite you...
Try putting data-no-turbolink="true" into the link that called the table page.
<a href="/vender" data-no-turbolink="true">
That works form me.
For rails 5, try using data: { turbolinks: false } inside any links to the page containing the form.
E.g. <%= link_to "Get in Touch", 'contact', data: { turbolinks: false } %>
I have to share my experience :
I played with Turbolinks, like you. But suddently, I had an issue : the previous pages needed to have turbolinks disabled too, to work.
After many & many & many hours on the issue, I found the solution :
<% f.submit %> was separated by 2 <div>from the rest of the form !
Here is an example:
Wrong:
<div class="container">
<div class="row">
<!-- Inscription -->
<div class="col-lg-8 contact_col">
<div class="get_in_touch">
<div class="section_title">Modifier une marque</div>
<div class="contact_form_container">
<%= form_for #brand, url: {action: "update"} do |f| %>
<div class="row">
<div class="col-xl-12">
<!-- Name -->
<label for="contact_name">Nom de la marque</label>
<%= f.text_field :brand, class: "contact_input" %>
</div>
<div class="col-xl-12 last_name_col">
<span>
<%= f.label "Image de la marque" %><br />
</span>
<%= f.file_field :brand_picture %>
</div>
</div>
</div>
</div>
<button class="newsletter_button trans_200">
<%= f.submit "Modifier" %>
</button>
<% end %>
</div>
</div>
</div>
Correct:
<div class="container">
<div class="row">
<!-- Inscription -->
<div class="col-lg-8 contact_col">
<div class="get_in_touch">
<div class="section_title">Modifier une marque</div>
<div class="contact_form_container">
<%= form_for #brand, url: {action: "update"} do |f| %>
<div class="row">
<div class="col-xl-12">
<!-- Name -->
<label for="contact_name">Nom de la marque</label>
<%= f.text_field :brand, class: "contact_input" %>
</div>
<div class="col-xl-12 last_name_col">
<span>
<%= f.label "Image de la marque" %><br />
</span>
<%= f.file_field :brand_picture %>
</div>
</div>
<button class="newsletter_button trans_200">
<%= f.submit "Modifier" %>
</button>
<% end %>
</div>
</div>
</div>
</div>
</div>
Think your workaround might be to just reload on form submit. So add remote: true to your form. This will just do a quick refresh for you. Please note that this is not a solution but a workaround. A recommended solution would be Garrett Berneche's answer.
<%= form_for(form_objects, html: {class: "form-in-table"}, remote: true) do |f| %>

rails flash.now from partial

I have a controller that is supposed to display a flash message. The flash message is not displaying.
My only problem is the flash messages not showing up. If anyone could please give an explanation for this that would be awesome. I am guessing that my error is noobish but i do not know enough to explain what is happening.
Edit: I added my Log for this section
Edit: When i click the manage button with invalid information nothing happens. When i refresh the page the flash message shows up. I really just need the current page refreshed but it is not working. If anyone has any ideas please share them.
Edit: I have tried changing things to
redirect_to #department, :notice => "invalid Password"
I am still not able to get the flash message to show up.
EDIT: From this site it says flash now will be displayed in the view that you are rendering. But my rendered view is a partial. Can anyone at least let me know if i am on the right track. A little direction or thought other than my own?
This is the create method for my relationship.
def create
#department = Department.find(params[:manager_relationship][:department_id])
if #department.authenticate(params[:manager_relationship][:password])
current_user.manage!(#department)
respond_to do |format|
format.html { redirect_to #department }
format.js
end
else
redirect_to department_path#department, :alert => "Invalid Password"
end
end
This same code is used successfully in my sessions controller.
SessionsController
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
It is called from a partial
department/show.hmtl.erb
<% provide(:title, #department.department_name) %>
<div class="span8">
<%= render 'manage_form' if signed_in? %>
</div>
_manage_form.html.erb
<div id="manage_form">
<% if current_user.managing?(#department) %>
<%= render 'unmanage' %>
<% else %>
<%= render 'manage' %>
<% end %>
</div>
_manage.html.erb
<%= form_for(current_user.manager_relationships.build(department_id: #department.id), remote: true) do |f| %>
<div>
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div><%= f.hidden_field :department_id %></div>
<%= f.submit "Manage department", class: "btn btn-large btn-primary" %>
<% end %>
application.html.erb
<!DOCTYPE html>
<html>
<head>
.
.
.
...
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
Here is the log for an unmanage and manage http request
Started DELETE "/manager_relationships/22" for 127.0.0.1 at 2013-03-24 11:08:20 -0700
Processing by ManagerRelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y3I7qY4LBfChjdYAUlJ1eDh23YkcJRwSRfa6s2wavoI=", "commit"=>"Unmanage", "id"=>"22"}
[1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = '9J1RAJy16Tooz5wMmzQohw' LIMIT 1[0m
[1m[35mManagerRelationship Load (0.0ms)[0m SELECT "manager_relationships".* FROM "manager_relationships" WHERE "manager_relationships"."id" = ? LIMIT 1 [["id", "22"]]
[1m[36mDepartment Load (1.0ms)[0m [1mSELECT "department".* FROM "departments" WHERE "department"."id" = 1 LIMIT 1[0m
[1m[35mManagerRelationship Load (0.0ms)[0m SELECT "manager_relationships".* FROM "manager_relationships" WHERE "manager_relationships"."user_id" = 1 AND "manager_relationships"."department_id" = 1 LIMIT 1
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mSQL (0.0ms)[0m DELETE FROM "manager_relationships" WHERE "manager_relationships"."id" = ? [["id", 22]]
[1m[36m (26.0ms)[0m [1mcommit transaction[0m
Rendered departments/_manage.html.erb (2.0ms)
Rendered manager_relationships/destroy.js.erb (5.0ms)
Completed 200 OK in 86ms (Views: 11.0ms | ActiveRecord: 27.0ms)
Started POST "/manager_relationships" for 127.0.0.1 at 2013-03-24 11:08:23 -0700
Processing by ManagerRelationshipsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Y3I7qY4LBfChjdYAUlJ1eDh23YkcJRwSRfa6s2wavoI=", "manager_relationship"=>{"password"=>"[FILTERED]", "department_id"=>"1"}, "commit"=>"Manage Bar"}
[1m[35mUser Load (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."remember_token" = '9J1RAJy16Tooz5wMmzQohw' LIMIT 1
[1m[36mDepartment Load (0.0ms)[0m [1mSELECT "departments".* FROM "departments" WHERE "departments"."id" = ? LIMIT 1[0m [["id", "1"]]
Redirected to http://localhost:3000/departments/1
Completed 406 Not Acceptable in 91ms (ActiveRecord: 0.0ms)
First, i can't see in your views where do you show the flash. You should do something like this in the view you want to display the message:
<% if flash[:success] %>
<p>
<%= flash[:success] %>
</p>
<% end %>
<% if flash[:error] %>
<p>
<%= flash[:error] %>
</p>
<% end %>
Second, when you do a redirection you should use flash[:success] = 'message' instead of flash.now[:success]. Use flash.now is you are not redirecting the user but just rendering a view.

Resources