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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:53:05+00:00 2026-05-17T18:53:05+00:00

I have the following in a form: <%= f.hidden_field (:project_id, :value => @project.id) %>

  • 0

I have the following in a form:

<%= f.hidden_field (:project_id, :value => @project.id) %>

This form partial sometimes contains a @project. Sometimes it does not. This is the nature of the app.

But I want to use one partial because it’s a big form. Problem here is if it does not contain @project, the page ERRORS….

How can I do the following:

  • Only render the hidden field if @project is defined
  • If @project is not defined, give the field a value of =””

Thank you!

  • 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-17T18:53:06+00:00Added an answer on May 17, 2026 at 6:53 pm

    You seem to be asking a couple different things. If you want to conditionally render the field, meaning the field doesn’t appear at all if @project is undefined, do this:

    <%= f.hidden_field(:project_id, :value => @project.id) if @project %>
    

    If you want the value to be blank when there is no project, you can trim down the previous answer’s conditional a little:

    <%= f.hidden_field (:project_id, :value => @project ? @project.id : '') %>
    

    There’s no need to check that @project is both defined and not nil, since it’s a global variable and calling it directly won’t trigger a “no method” error.

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

Sidebar

Related Questions

I have the following form in a Struts2 JSP that contains some radio buttons.
I have the following form: echo $this->Form->input('Person.name'); echo $this->Form->input('Person.phone'); echo $this->Form->input('Person.email'); echo $this->Form->input('Message.0.plainmsg'); echo
I have the following form <form name=myForm id=myForm method=post enctype=multipart/form-data action=script.php> and this jQuery
I have the following two fields in a form: <input type=button value=Rep id=rep name=rep
I have the following form and I am trying to get the value of
I have the following form: <%=form_for [:project, @permission], :url => { :action => update
I have the following form: @model Teesa.Models.SearchModel @using (Html.BeginForm(Index, Search, FormMethod.Get, new { id
I have the following form and controller. <%= form_tag do %> <div> <%= label_tag
I have the following form, which I have reduced as much as I can,
I have the following form for photo_album which uses the nested forms feature of

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.