I have checked old similar post but but I'm still having problems.
When I create a new listing the URL looks as below: http://localhost:3000/listings/new?plan=1
In the form_for I'm hiding some fields when plan=1
<%= form_for(@listing) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :plan, :value => @plan %>
<%= f.label :Title %>
<%= f.text_field :title, class: 'form-control' %>
<!-- don't show description for standard plan -->
<% if @plan != 1.to_s %>
<%= f.label :Description %>
<%= f.text_area :description, :rows => 10,
placeholder: "Describe your business...", class: 'form-control' %>
<% end %>
In the ListingsController I set the plan in before action:
before_action :set_plan, only: [:new, :create]
def set_plan
@plan = params[:plan]
end
so I can use it as hidden field on the form above.
My problem starts when I save the form with errors. The new form URL changes to http://localhost:3000/listings. I can see the @plan=1 still on the form:
<input value="1" type="hidden" name="listing[plan]" id="listing_plan" />
, but now the form is showing all fields (which should be hidden due to this condition <% if @plan != 1.to_s %>)
Actions in ListingsController:
def new
@listing = Listing.new
end
def create
@listing = current_user.listings.build(listing_params) if logged_in?
@plan = @listing.plan
if @listing.save
flash[:success] = "Listing created!"
redirect_to @listing
else
render 'new'
end
end
I have tried something like that but no success: render :action => 'new', :plan => @listing.plan and few other things
How can I hide those fields on error?
@plan
, rather useparams[:plan]
– davegson Oct 21 '17 at 10:47