views:

33

answers:

2

Hi,

In an app I have the following:

 def new
    @property = Property.new(:country_id => 1, :user_id => current_user.id, :status_id => 'draft')
  end

  def create
    @property = Property.new(params[:property])
      if @property.save
        flash[:success] = t('The_property_is_successfully_created')
        redirect_to myimmonatie_url
      else
        flash.now[:error]=t("The_property_could_not_be_created")
        render :action => 'new'
      end
  end

When an error accors, the line render :action => 'new' gets executed, but the my form gives an error: user blank country blank These cannot be blank (defined in model), meaning this code:

@property = Property.new(:country_id => 1, :user_id => current_user.id, :status_id => 'draft')

is not executed anymore. What is the reason and solution?

UPDATE: The view is very long, but the relevant lines are these:

<% form_for :property, @property, :url => { :action => "create" } do |f| %>
<%= f.error_messages( :header_message => nil, :message => nil) %>
    <!--lot's of fields -->
<%- end -%>

The app does not crash, it is the error_messages that show the fields that cannot be blank. The first 2 answers already gave me the reason for the error: the render new does not execute the new method, so it is logical that the paramters are empty. The goal is now to make sure that the view also reveices the parameters that are also set in the new method. How to achieve that?

Thanks!

A: 

You are right the code from the method 'new' is not being executed. When you call render :action => 'new' it will just render the view for 'new' action (i.e. new.html.erb). It will not execute 'new' method again.

However you have this line:

@property = Property.new(params[:property])

Which should fill out @property with all the params from the form, so the form should be filled ok.

My guess is that your initial form is not good (the form in new.html.erb). Maybe you are not filling out params properly or submitting them. Can you post new.html.erb code?

Slobodan Kovacevic
it is a very long form:)But I guess this one line is the most important:<% form_for :property, @property, :url => { :action => "create" } do |f| %>
PlanetMaster
indeed, you see I'm not experienced. I see the reason.I should put your line of code before the form_for line in the view?Or is there a better way integrated the line of form_for?
PlanetMaster
That seems ok. Message that you are getting means that on @property.save validation failed, which in turn means that @property = Property.new(params[:property]) didn't "work" or that params doesn't have all the data for property. I would say that something is wrong with the form itself (i.e. input fields). Checkout the Rails app log and see what Rails is getting in the params hash on request.
Slobodan Kovacevic
yes, you are right, but the error is unrelated to this. That is another issue that I still need to figure out. The error says that the current user is not allowed to create a property. I think that is someting in routes. But first I wanted to tackle this bug. I just need the view to pick up the 3 parameters again, the other submitted data from the form is entered correctly again.
PlanetMaster
adding your line to the view fixes the error, but causes the drop down boxes in the form to loose the value after the render new.text_fields are ok.example of a select that looses the value:<%= f.select(:province_id, options_from_collection_for_select(Province.find(:all, :conditions => { :country_id => @property.country_id }), :id, :name)) %>
PlanetMaster
It's not my line; you have that same line in your code - and there is definitely no reason to put it in your view. The problem with your select fields is that you are using options_from_collection_for_select without 4th param which sets selected value (see: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001628). You could also ditch it completely and do everything through simple select. Something like this: <%= f.select(:province_id, Province.find(:all, :conditions => {:country_id => @property.country_id}).collect {|p| [p.name, p.id]}) %>
Slobodan Kovacevic
A: 

Your problem here, according to guides.rubyonrails.org, is this:

Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render.

I suppose that's fairly logical, it's only rendering the template, not performing the action. You may want to try redirect_to, but that will likely lose your submitted data.

Stephen Orr
that is indeed the reason, and I'm a newcomer also :)Is there a way to pass the view the parameters I set in the new method? I guess I could with hidden form fields, I was first tryinng that. But it seemed that adding html is not best practice.
PlanetMaster