简体   繁体   中英

HTML select tag allowing you to access Rails variable

    <p class="acp3">
    <label>Status</label>
       <select>
        <% if <option>Active</option> %>
            <%= account["status"] == "Active" %>
        <% elsif <option>Disabled</option> %>
            <%= account["status"] == "Disabled" %>
        <% end %>
       </select>
</p>

I am creating a selector with the html and I want to be able to have it access that variable when the specific selector is choosed. Is there a way to do this with Rails or is there a javascript way of doing it?

I'm confused. Do you mean want something to happen as soon as the user chooses one of those options, before the form is submitted? If so, you need to do it with Javascript. Ruby isn't running on the user's Web browser.

If you mean you want one of those options to be pre-selected based on the value of the account status, that's certainly something Rails can do. But in that case, your "if" statement is backwards. See the documentation for the Rails select_tag helper; it'll make your life a lot easier.

The ruby code is trying to capture the value you selected and posted(?), or you are trying to do something different? Also any reason you are creating the raw HTML directly rather than just using the select_tag helper?

I figured out how to do it I needed to do this.

<select name="status">
                        <option value="active">Active</option>
                        <option value="disabled" selected="selected">Disabled</option>

that allowed me to access the options like I wanted too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM