Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document Request.js in the guides [ci-skip] #47365

Merged
merged 1 commit into from Apr 9, 2023

Conversation

p8
Copy link
Member

@p8 p8 commented Feb 11, 2023

With the removal of UJS as a default dependency, it can be unclear what to use for Ajax calls. The Rails Request.js is the official library to use in this case, so it should be documented in the guides.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the docs label Feb 11, 2023
@p8 p8 force-pushed the guides/document-request-js branch 2 times, most recently from 4a6e5f9 to c82ce11 Compare February 12, 2023 14:24
When using another library to make Ajax calls, it is necessary to add the
security token as a default header for Ajax calls in your library. To get the
token, have a look at <meta name='csrf-token' content='THE-TOKEN'> tag printed
by <%= csrf_meta_tags %> in your application view.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think users should be copy/pasting the output of csrf_meta_tags in their code as it will break the next time the secret changes.

I'm not sure the right approach is here though off the top of my head.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document.head.querySelector("meta[name=csrf-token]")?.content does the trick (adapted from here)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bdewater !
I copied the original text from the security guide. I'll change it there as well.

@p8 p8 force-pushed the guides/document-request-js branch 4 times, most recently from 9706dd2 to e3d5472 Compare February 13, 2023 21:12
With the removal of UJS as a default dependency, it can be unclear what
to use for Ajax calls. The Rails Request.js is the official library to
use in this case, so it should be documented in the guides.
@p8 p8 force-pushed the guides/document-request-js branch from e3d5472 to 83574a7 Compare February 13, 2023 21:19
@@ -300,3 +301,43 @@ added to the form that the `button_to` helper renders internally:
```erb
<%= button_to "Delete post", post, method: :delete, form: { data: { turbo_confirm: "Are you sure?" } } %>
```

### Ajax Requests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a link to the security doc with more detail on CSRF?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will follow up on this after.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zzak zzak merged commit 4e41701 into rails:main Apr 9, 2023
@p8 p8 deleted the guides/document-request-js branch April 9, 2023 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants