开发者

Writing proper validation errors?

Update : Gutted entire question with more thorough description

Ok same question with different names.

In my model, I do validate the presence of.

class QuickFact < ActiveRecord::Base
 belongs_to :organization

 validates_presence_of :quick_fact, :content

But if either is blank, it errors out with :

Missing template organizations/_quick_fact_fields.erb

Here's the catch. I have a nested form model with dynamically addable parts to it. As followed from here :

http://rails开发者_开发技巧casts.com/episodes/197-nested-model-form-part-2

That is what generates and calls the _quick_fact_fields.erb . But that works perfectly and is located within quick_facts/_quick_fact_fields.html.haml

Update: My Controller Code

organizations_controller.rb

def update
  if @organization.update_attributes(params[:organization])
    ..
    elsif params[:organization][:quick_facts_attributes]
      flash[:notice] = 'QuickFacts successfully updated.'
      redirect_to organization_quick_facts_url(@organization)
    else
      flash[:notice] = 'Organization was successfully updated.'
      redirect_to :action => 'edit'
    end
  else
    # re-render last form
    ..
    elsif params[:organization][:quick_facts_attributes]
      render :template => "quick_facts/index"
    else
      render :action => 'edit'
    end
  end
end


It seems that you're trying to render a my_custom_field partial from one of the worker views found in app/views/worker, but apparently there's no such partial there. If you show us the code of the relevant views and controllers, it will be easier to pinpoint the exact problem.

On a side note, you could simply do validates_presence_of :name instead of defining a custom validation method to simplify your model. However, this is likely unrelated to the error you're describing and is just a general improvement suggestion.


Got it. I had two controllers.

quick_facts_controller.rb, and organizations_controller.rb

Once I deleted the update function in quick_facts_controller, it worked properly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜