Rails: Pass parameters with render :action?

前端 未结 4 1589
时光取名叫无心
时光取名叫无心 2020-12-08 07:14

I have a form that displays differently depending on the parameter it was called with.

Ex.

testsite.local/users/new?type=client

So

4条回答
  •  一生所求
    2020-12-08 07:46

    There are two ways to do it.

    Either add a hidden field or an extra parameter to the form_for

    Adding hidden field in your form containing named type and set it equal to params[:type] this will preserve the type on the render if validation fails.

    View code:

    <% form_for @user do |f| %>
    ...
    <%= hidden_field_tag :type, params[:type] %>
    <%end%>
    

    Adding an extra paramater to the form:

    <% form_for @user, create_user_path(@user, :type => params[:type]) %>
    ...
    <%end%>
    

    Either one will do what you want it to.

提交回复
热议问题