Automatically populate form fields in Rails?

Suppose you had a simple form for creating a new article object in your application.

<% form_for @article do |f| %> <p> name:<br /> <%= f.text_field :name %> </p> <p> link:<br /> <%= f.text_field :link %> </p> <p> <%= submit_tag %> </p> 

I use Feedtools RSS parser to get article names, but I cannot automatically fill out form fields from data available elsewhere. Say the title of the article is accessible via params[:name] . How can I get the article title from params[:name] (or params[:link] , for that matter), in the form field if the user should not enter it? I do not want to automatically create an article object, because the user can change the name a little.

+4
source share
1 answer

If you pass in the information that you want to display in the Article constructor in the new action, the form will display completed. Even if a new object was created, it will not be saved in db, because in no case was the save method called. This will happen in the create action.

 def new @article = Article.new :name => "Steve Graham insane blog", :link => "http://swaggadocio.com/" respond_to do |format| format.html # new.html.erb format.xml { render :xml => @post } end end 

Without knowing more about your application logic, I can no longer help on how to stitch the details together. That should bring you almost to you.

+11
source

All Articles