Update field through link_to in Rails
Try
<%= link_to "Remove", update_products_path(product:{parent_id: nil}), method: :put %>
There is no HTTP-Verb update
what you need is put
. You can read about Rails and HTTP-Verbs
here
update field from a link_to in rails 4
I finally got to fix this by just changing the link from:
<td><%= link_to 'Publish', post_path(post, visible: true), method: :put %></td>
to this:
<td><%= link_to 'Publish', post_path(post, post: {visible: :true}), method: :put %></td>
rails link_to update 1 field
There's no automatic way to do this through a view helper such as link_to. Your actions in your controller need to actually do this for you.
If you never want to delete a document through your destroy action, then just rewrite that action to set its 'deleted' attribute to true instead of actually destroying it. That way you can continue to use your original link. so:
class Document < ActiveRecord::Base
# DELETE /documents/:id
def destroy
@document = Document.find(params[:id])
@document.deleted = true
@document.save
redirect_to documents_path
end
end
with
<%= link_to 'Destroy', document, :confirm => 'Are you sure?', :method => :delete %>
Rails: Update data via link_to (without view)
You need to change rooms_path
to room_path
.
Without params:
<%= link_to "Delete", room_path(room) , method: :put, data: { confirm: "You sure?"} %>
With params:
<%= link_to "Delete", room_path(room, room: {name: nil, address: nil}) , method: :put, data: { confirm: "You sure?"} %>
Rails 4 : Table Boolean Column Update Using link_to with a specific value TRUE always
You can pass hidden params through button_to
:
<%= button_to "Update", user, method: :put, params: { doc_delete: true } %>
This will create a micro-form, much like what Marwen
alluded to. Whilst quite inefficient, it will be the best way to send data to your update
action.
--
Another, more efficient, way would be to define a custom route/action:
#config/routes.rb
resources :customers do
patch :doc_delete, on: :member #-> url.com/users/:id/doc_delete
end
#app/controllers/customers_controller.rb
class CustomersController < ApplicationController
def doc_delete
@customer = Customer.find params[:id]
redirect_to customers_path if @customer.update doc_delete: true
end
end
#app/views/customers/index.html.erb
<% @customers.each do |customer| %>
<%= link_to "Update", customer_doc_delete_path(customer) %>
<% end %>
Rails link_to update attribute from controller action
There are several issues with your code
Since you are updating a record you need to include
method: :patch
in your link and also you need to modify the routes file to include appropriate route.In your routes file add
#routes.rb
resources :meetings do
member do
patch 'completed'
end
endIn your view
<%= link_to "Mark as completed", completed_meeting_path(@meeting), remote: true, method: :patch %>
Since you have a
remote:true
in your link so rails will try to render a javascript template and since you are not rendering a javascript template you will get a template missing errorIn your controller
def completed
@meeting = Meeting.find(params[:id])
respond_to do |format|
# if something ...
# update meeting record and then render js template
format.js {render :status => :ok} # it will render completed.js.erb inside 'app/views/meetings/' directory
# else
# something else
format.js {render :action => "different_action"} # it will render different_action.js.erb inside 'app/views/meetings/' directory
end
end
Now all you need to do is to write some basic javascript in the js templates to update the view.
Note : If you don't want to render anything simply use format.js { render :nothing => true }
.
Also if you want to use redirect_to
calls instead of rendering js template just remove remote: true
form you link .
Rails 3 how do I use link_to to change the value of a boolean in the db?
You can update the code below
You View
<% @users.each do |user| %>
<%= link_to 'Approve', active_user_path(user) %>
<% end %>
You Controller
def activate
@user = User.find(params[:id])
if @user.update_attribute(:approved, true)
redirect_to "something"
else
render "something"
end
end
Your routes
match "users/:id/activate" => "users#activate", :as => "active_user"
Hope can help you.
Rails - Update a single attribute : link with custom action or form with hidden fields?
Not sure what your routes look like but if you have an update action it should work with the link_to. If you go with the link make sure you use a method of PUT or POST (ideally PUT because it's an update):
link_to("Unlink your facebook account", user_path(@user, :facebook_uid => nil), :method => :put, :confirm => "Are you sure?")
Update a boolean field in db using link_to
link_to
can only link to a target controller action. You need to define a route that will route the call to a method that toggles the boolean value.
For example, in your controller:
class ThingsController
def toggle_foo
@thing = Thing.find(params[:id])
@thing.foo = !@thing.foo
@thing.save
end
end
Then you would route this:
resources :things,
:member => { :toggle_too => :put }
Then you can link to it:
link_to('Toggle', toggle_foo_thing_path(@thing), :method => :put)
It's important to not use a GET
method on these calls because some browsers will pre-load all the simple links on your page, which will have the effect of automatically toggling all those things you link to on the page.
update a product attribute with link_to in my admindashboard rails
You could set the route to accept an id from the params:
patch '/publish_product/:id' =>'pages#publish_product'
Then in the link_to you use that path plus the id:
<% @notactives.each do |product| %>
...
<%= link_to 'publish the article', publish_product_path(product.id) %>
<% end %>
And in the controller you "flips" the attribute based on the current value, like:
def publish_product
@product = Product.find(params[:id])
@product.active = !@product.active
redirect_to :admindashboard if @product.save
end
Related Topics
"/#Action" Route in Routes.Rb in Ruby on Rails
Actionmailer Smtp "Certificate Verify Failed"
Ruby CSV.Each in While Loop Not Executing Second Time Through
Uploading a File to a S3 Presigned Url
Rails: Merit Gem Badge Not Registering or Displaying
How to Use Ruby Date Constants Gregorian, Julian, England and Even Italy
Loop Within Loop in Rails Controller
Ruby Daemon Process to Keep Objects Alive for Transient Ruby Instances
Ror: Meaning of "User ||= User.New"
Ruby Selenium Web Driver: How to Count Child Element Nodes of a Specific Node
Has_Many and No Method Error Issue
Rails 6: Only One Profile Per User Should Be Created
Confused with Ruby Accessor Methods
Getting Error (Exit Code 2) for Bootsnap, Not Allowing to Set Up Rails Server