How do you apply a patch to ruby on rails?
You typically don't want to patch the gem source itself. You probably will want to freeze the gems into ${RAILS_ROOT}/vendor/rails
, and then apply the patch locally.
From your ${RAILS_ROOT} dir, dump all of your rails gems into vendor/rails
rake rails:freeze:gems
Apply the patch
cd vendor/rails/
patch -p1 < 0001-Fix-implicit-multipart-mailer-views-when-RAILS_ROOT.patch
Patch a Ruby Gem
Yes, it's possible. Just open the class, alias the method that has problems, and provide your own implementation of it. This page shows an example of this.
You can open that class from any class, provided you added the necessary includes. Physically, the original code will remain unchanged.
Does rails use method: :patch for editing with _form.html.erb?
As mentioned in the tutorial you shared:
The reason we can use this shorter, simpler form_for declaration to stand in for either of the other forms is that @article is a resource corresponding to a full set of RESTful routes, and Rails is able to infer which URI and method to use.
Also, from the reference given there:
For an existing resource
or record
for @post
, it is equivalent to:
<%= form_for @post, as: :post, url: post_path(@post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
...
<% end %>
Whereas, for a new record of the resource, i.e. Post.new
, it is equivalent to
<%= form_for @post, as: :post, url: posts_path, html: { class: "new_post", id: "new_post" } do |f| %>
...
<% end %>
Thus, in this case, magic lies in the rails with 2 things, helping rails understand what to do with this form:
- Your defined routes for your model,
resources :article
- The type(Existing record or a new record) of
resource
object passed toform_for
.
Button_to pointed to a patch/update method doesn't works
Briefly : Change action: 'remove'
to action: 'destroy'
.
Most browsers only support GET, POST, OPTION but all the other methods are emulated by Rails using hidden form field _method.
When you say method: 'patch'
it means that in request.method
in you controller's method it will be patch
but in if you want to specify the exact method in the controller you should use action
Hope that will help :)
Rspec: How to properly make patch request
Okay figured it out. First I made a factory for the habit just to clear things up. I kept messing with the syntax and came to the correct answer.
form_params = {
params: {
name: "new",
description: "shit"
}
}
patch :update,id: 1, form_params
This was telling me wrong number of arguments and I eventually realized I needed to pass the id with my form_params. Like I said, small mistake.
Correct code:
form_params = {
id: 1,
name: "new",
description: "shit",
}
patch :update, params: form_params
Related Topics
No Database Connection in Rails Console
Simple_Form: Remove Outer Label for an Inline Checkbox with Label
Ruby Class with Static Method Calling a Private Method
Generate a Migration File from Schema.Rb
How to Use Ruby Minitest::Spec with Rails for API Integration Tests
Post JSON Data to Simple Rails Application with Curl
What's the Most Efficient Way to Deep Copy an Object in Ruby
Should I Use Class Method or Instance Method, and Why
Cucumber, Capybara and Selenium - Submitting a Form Without a Button
Errno::Enoent: No Such File or Directory Ruby
Carrierwave: Create the Same, Unique Filename for All Versioned Files
How to Get the Ruby Documentation from the Command Line
How to Test a Ruby Application Which Uses Mechanize