开发者

How do I pass object values with render :action => 'new'

开发者 https://www.devze.com 2023-01-01 03:08 出处:网络
In an app I have the following: def new @property = Property.new(:country_id => 1, :user_id => current_user.id, :status_id => \'draft\')

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!


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.

Try the following (note that I haven't actually run this code, but it looks correct!):

def new
  @property = Property.new(default_params)
end

def create
  @property = Property.new(params[:property].merge(default_params))
  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

def default_params
  {
    :country_id => 1,
    :user_id => current_user.id,
    :status_id => 'draft'
  }
end


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消