I'm making an EC site with Rails6.
I want to select a product from the product (ticket) list page, click the button to change the page, and display a list of selected products.
First, the products selected on the product list page will be entered into ticketIds as an array in JavaScript.
Then, when the button is pressed, the following jQuery statement is invoked and a get request is sent to the controller below.
$. ajax ({
type: "GET",
url: "/ manage_tickets /",
data: {id: ticketIds},
dataType: "html"
}
});
class ManageTicketsController<ManageController
before_action: logged_in_user, only:% i [index create destroy]
before_action: correct_user, only:: destroy
def index
@tickets = Ticket.where (id: params [: id]). limit (10) #This is a test
end
When you look at the log, you can see that the rendering is done without any problems.
D, [2019-11-29T21: 02: 57.198735 # 83105] DEBUG-: ↳ app/views/manage_tickets/index.html.erb: 22
I, [2019-11-29T21: 02: 57.199729 # 83105] INFO-: Rendered manage_tickets/index.html.erb within layouts/manage (Duration: 257.7ms | Allocations: 17416)
I, [2019-11-29T21: 02: 57.215902 # 83105] INFO-: Rendered layouts/_header.html.erb (Duration: 3.5ms | Allocations: 1051)
I, [2019-11-29T21: 02: 57.216350 # 83105] INFO-: Rendered layouts/_sidebar.html.erb (Duration: 0.2ms | Allocations: 132)
I, [2019-11-29T21: 02: 57.217211 # 83105] INFO-: Rendered layouts/_footer.html.erb (Duration: 0.7ms | Allocations: 462)
I, [2019-11-29T21: 02: 57.217458 # 83105] INFO-: Completed 200 OK in 281ms (Views: 217.5ms | ActiveRecord: 60.6ms | Allocations: 24203)
But nevertheless, the page doesn't change and remains the original product selection screen.
Why is it? .
What I did・ In the first jQuery description, there is dataType: "html", but since it was first datatype, it was modified, but it has not changed.
・ There is no error, but I tried to put the following in front of the above ajax statement by copy and paste, but it is not changed because it is because the token is not included.
$.ajaxPrefilter (function (options, originalOptions, jqXHR) {
var token;
if (! options.crossDomain) {
token = $('meta [name = "csrf-token"]'). attr ('content');
if (token) {
return jqXHR.setRequestHeader ('X-CSRF-Token', token);
}
}
});
I deleted turbolinks from the page application.js, but ...
-
Answer # 1
Related articles
- ruby on rails 5 - i want to update the received parameters with nil
- ruby on rails - rails update not possible
- ruby on rails 6 - [rails] i want to update the display of chat room participants etc in real time
- ruby - when i try to update rails, the empty value is saved
- ruby - when i try to update rails, i get a routing error
- ruby on rails 5 - can i update with just the update action?
- ruby on rails - i can't update rubygems
- ruby - data is not updated by update action rails
- ruby on rails 6 - about rails routing error no route matches [get] "/"
- ruby - about implementation of tag function in rails
- ruby on rails - i want to display the posted images in a random order (get information from the record)
- ruby - [rails] i want to change the argument of link_to using conditional branching
- ruby on rails - [rails] i'm implementing a search function using form_with, but i'm having trouble with the search results not b
- ruby on rails - i would like to add an automatic address input function to the new registration screen
- ruby - [rails] i want to find the date difference using the model created_at
- ruby on rails 6 - rails6 activerecord :: recordinvalid in userscontroller # update
- ruby on rails - i want to resolve rails wrong number of arguments
- ruby on rails - questions about routes
- ruby - about "activemodel :: unknownattributeerror" of rails
- ruby - rails causes activerecord :: recordnotfound and routing error at the same time
- javascript - jquery doesn't work on heroku
- ruby - asynchronous communication 500 (internal server error)
- javascript - [rails] css and js do not respond when material-dashboard is installed
- ruby - i want to perform asynchronous communication uncaught error: syntax error, unrecognized expression: # like-button-# {@
- ruby - unsolved i want to make the like function asynchronous if you reload it, it will be read
- ruby on rails - about the error when trying to implement jquery with rails ($is not defined;please fix or add/* global * /)
- ruby on rails - uncaught reference error: $is not defined cannot be resolved
- ruby on rails - i'm using payjpjs v2 i want to register the card name
- ruby - i want to make a graph of the values retrieved by the each statement with chart kick
Ajax performs server-side processing without page transitions.
dataType only specifies the type of data returned from the server side.
Normally, the information returned from the server side is rewritten with JavaScript using JavaScript.
First Ajax (jQuery) 2018 edition
As you can see, if communication/processing is successful, you will receive the result with done () It is a good idea to check the returned results and reflect them in HTML accordingly.