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 8969079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:32:35+00:00 2026-06-15T17:32:35+00:00

How can I set a field of an object to a fixed value using

  • 0

How can I set a field of an object to a fixed value using a button or link in Rails? It seems like this should be a fairly basic procedure, but I haven’t been successful with it. An example would be to set the parent_id of a product model to nil (related earlier question Update field through link_to in Rails). I have managed to achieve this through a form by leaving the text field empty:

<%= form_for @product do |f| %>    
    <%= f.label :parent_id %>
    <%= f.text_field :parent_id %>
    <%= f.submit "Update", class: "btn" %>  
<% end %>

But how can this be done without a text field, simply using a button, so that the parent_id is set to nil when the button is pressed? I have also tried to set the parent_id in a hidden field, but that doesn’t work.

<%= form_for @product do |f| %>
   <%= f.hidden_field parent_id: nil %>
   <%= f.submit "Remove", class: "btn" %>
<% end %>

The controller looks like this for the edit/update actions.

products_controller.rb

def edit
   @product = Product.find_by_id(params[:id])    
end

def update
  if @product.update_attributes(params[:product])
    flash[:success] = "Product updated"
    redirect_to @product
  else
    render 'edit'
  end
end

Edit

See Update field through link_to in Rails for how to do this using link_to instead of a form.

  • 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-06-15T17:32:35+00:00Added an answer on June 15, 2026 at 5:32 pm

    Try

    <%= f.hidden_field :parent_id, value: nil %>
    

    instead of

    <%= f.hidden_field parent_id: nil %>
    

    And make sure parent_id is in attr_accessible list of the model.

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

Sidebar

Related Questions

like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
How can I give set foreign key value field in Entity Framework. I have
How can I programatically set a field value by its name for a google
I would like to set a field to a value selected from a grid
If I am creating say a input field through MXML , I can set
How can I set a Microsoft SQL field as identity but to START with
Can I change the field public virtual ClassOne ClassOne { get; set; } to
.NET I want to clone a value type's fields. How can i set a
I am using method static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true);
Query: UPDATE item_table SET field1=field1_spanish, field2=field2_spanish; Question: How can I update field1 with field1_spanish

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.