Processing two unassociated Rails models' forms in one controller action -
i'm creating rails app take in input 2 models , respective forms, , log website mechanize , hundreds of tasks there.
the first form consists of user's login information (the username , password, using model user), , second 1 long list of term names , respective definitions (using model term , uploaded form excel doc).
i have no need associate these 2 models (unlike in similar questions, seem deal nested models). once mechanize task completes, destroy both models' objects database.
the 2 pieces of app (logging in website; , uploading terms , using them interact website) both work in separate scripts.
my question is: how can take in both models' information on 1 webpage , coordinate controller(s) accordingly? in situation, can create both objects in 1 controller? (if not, that's fine, long there's alternative; i'm game whatever work.)
i'm posting of code below. i'm happy answer questions may have. please keep in mind pretty new @ rails, not elegant code:
the user model:
class user < activerecord::base     attr_accessor :username, :password end and term model:
class term < activerecord::base     attr_accessible :name, :definition end in terms controller (the forms located in index action , script run in show action):
def create  @term = term.new(params[:term])  @user = user.new(params[:user])    if (@term.save && @user.save)     flash[:notice] = "your terms have been sent processing."     redirect_to terms_path   else     render :action => 'index'   end end        def show    @term = term.all    agent = mechanize.new     page = agent.get('www.myurl.com')    #log website    @user.username = myform.field_with(:id => "userfield").value    @user.password = myform.field_with(:id => "passfield").value    #and enter term information website's forms    rest of mechanize code goes here...  end and in views/terms/index.html.erb:
#the login form: <%= render 'loginform' %>   #the term file uploader: <%= form_tag import_terms_path, multipart: true %>  <%= file_field_tag :file %> <%= submit_tag "import" %> <% end %>  <br> <table id="terms">   <tr>     <th>name</th>     <th>definition</th>   </tr>  #displays uploaded terms on index page <% @terms.each |term| %>    <tr>     <td><%= term.name %></td>     <td><%= term.definition %></td>   </tr> <% end %> </table>  <p><%= link_to 'update website these terms', terms_update_terms_path %></p> and in views/terms/_loginform.erb:
 <%= form_for(@user) |f| %>  <div class="field">   <%= f.label_tag(:username, 'username') %><br />   <%= f.text_field_tag(:user, :username) %> </div> <div class="field">  <%= f.label_tag(:password, 'password') %><br />  <%= f.password_field_tag(:user, :password) %> </div> <% end %> and in views/terms/_termform.html.erb
<%= form_for(@term) |f| %>   <div class="field">    <%= f.label :name %><br />    <%= f.text_field :name %>   </div>   <div class="field">    <%= f.label :definition %><br />    <%= f.text_area :definition %>   </div>   <div class="actions">    <%= f.submit %>   </div> <% end %> 
if need save data , don't want associate models, save term separately:
<%= form_for @user |f| %>     <div class="field">       <%= f.label :username, 'username' %>       <%= f.text_field :username %>     </div>     <div class="field">       <%= f.label :password %>       <%= f.password_field :password %>     </div>     <div class="field">       <%= label_tag :name %>       <%= text_field_tag :name %>     </div>     <div class="field">       <%= label_tag :definition %>       <%= text_field_tag :definition %>     </div>     <div class="actions">       <%= f.submit %>     </div> <% end %> then in controller:
@term = term.new  @term.attributes = { :name => params[:name], :definition => params[:definition] }  @term.save if adding multiple term fields form, consider cocoon, , add each loop saving terms in controller.
Comments
Post a Comment