Question

My models are: Projects has_many Feeds. I just added a column to my Feeds table called feed_error. I currently have a form on the app that creates a new Feed when entered. I want to be able to set feed_error to false by default. In my feeds_controller, I have my create method:

def create
@feed = Project.find(params[:project_id]).feeds.build(params[:feed])

respond_to do |format|
  if @feed.save
    format.html { redirect_to( :back, :notice => 'Feed was successfully created.') }
    format.xml  { render :xml => @feed, :status => :created, :location => [@feed.project, @feed] }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @feed.errors, :status => :unprocessable_entity }
  end
end
end

I was thinking I could try adding :feed_error => 'false' to the params, but that doesn't seem to work. How do I set this field by default?

Was it helpful?

Solution

You have a couple options. In your controller you can do:

def create
@feed = Project.find(params[:project_id]).feeds.build(params[:feed])
@feed.feed_error = false

respond_to do |format|
  if @feed.save
    format.html { redirect_to( :back, :notice => 'Feed was successfully created.') }
    format.xml  { render :xml => @feed, :status => :created, :location => [@feed.project, @feed] }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @feed.errors, :status => :unprocessable_entity }
  end
end
end

You could also set this up in your database migration. For example, if you don't need a null value and instead want the default to be false you can add:

t.boolean "feed_error", :null => false

to your migration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top