Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599312
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:21:07+00:00 2026-05-18T20:21:07+00:00

I’ve been stuck on this for a few days and I’m afraid I lack

  • 0

I’ve been stuck on this for a few days and I’m afraid I lack the vocabulary to properly ask this question, but any feedback is greatly appreciated!

I am working on a basic blog site. Users can create posts with a title, description, and declare whether others can see the post (public or private). From the index page, I want the user to be able to see a list of their posts, toggle public|private, and delete the post. Listing is easy (I’m rendering a partial based on the user’s posts), and the AJAX delete link is working nicely:

<%= link_to 'Delete Post', post, :confirm => 'Are you sure?', :method => :delete, :remote => true %>

Toggling public|private corresponds to an edit/update action, and I cannot figure out how to do that using a link or button–it seems I have to use a form. I’ve tried a lot of variations of the working delete button that look something like this (and none work):

<%= link_to 'Make Public', { :controller => "posts", :action => "update", :id => post, :public => true, :post => post }, :method => :put, :remote => true %>

The only way that seems to work is with a form, but this requires that the user click the Update Post button:

<%= form_for(post, :remote => true) do |f| %>
  <div class="field">
    <%= f.label :public %><br />
    <%= f.check_box :public %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

So, in Rail3 what is the proper way to update a single attribute? (Is there more fitting verbiage?) Can it be done using a link or button? Do I have to use a form? Is there a Rails3 way (like destroy.js.erb) to add an onclick/onchange event or do I need to develop my own JavaScript?


Zabba’s answer is great; however, I ultimately used a hidden form submitted via JavaScript (specifically jQuery).

In the view:

<div>
  <%= form_for(post, :remote => true) do |f| %>
    <%= f.hidden_field :public, { :value => !post.public } %>
  <% end %>
  <% if post.public == true %>
    <div class="pp">Public</div>
  <% else %>
    <div class="pp">Private</div>
  <% end %>
</div>

The JavaScript:

$('.pp').live('click', function(){
  $(this).siblings('form').submit();
});

Comments on the two approaches? Is one more idiomatic?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:21:08+00:00Added an answer on May 18, 2026 at 8:21 pm

    I would do it this way (not tested, so might be some minor mistakes):

    1. Create action named make_public in PostsController

      a. The link” would be: <%= button_to ‘Make Public’, :confirm => ‘Are you sure?’, :method => :put, :remote => true %>

      b. The action will receive the posts’s id (params[:id])

    2. Define a method called make_public on the Post model
      a. Call update_attributes :make_public => true

    The link that does the update should be a PUT not a GET (Always put all state-changing actions behind a POST or a PUT)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.