سؤال

Ruby on Rails 3, I have an edit page with two select_tag submissions. One has users that have the attribute :attend = "Yes" and the other has users that have the attribute :attend = "No" or nil. I am trying to get the select_tag submissions to create or update the Certificate table for the user. As of now I know it is POSTing the user id and name. I do not know how to get the controller or model to take the user id and create or update with the :attend attribute value of "Yes" or "No". The app will create a new record with the correct user id but not with :attend since it is not in my select_tag submission.

Here is my edit:

<%= form_for @untrained do |f| %>
            <p> Trained Users </p>
            <%= select_tag "certificate[user_id]", options_for_select(@current_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
            <%= f.submit "Un-Train", class: "btn btn-large btn-primary" %>
        <% end %>
        <%= form_for @trained do |f| %>
            <p> Non-Trained Users </p>
            <%= select_tag "certificate[user_id]", options_for_select(@non_trained.collect{|x| [x.name, x.id]}), {:multiple => :multiple} %>
            <%= f.submit "Trained", class: "btn btn-large btn-primary" %>
        <% end %>

Here is my controller:

@trained = Certificate.new(params[:certificate])
@untrained = Certificate.update(params[:user_id])

Here is my model:

attr_accessible :attend, :pass, :user_id
belongs_to :user
validates :user_id, presence: true

How do you set the controller to create or update a new record with :attend as "Yes"? How do you update the record with :attend as "No"? Thank you

هل كانت مفيدة؟

المحلول

Have u tried in the controller after

if @trained.save

put

@trained.update_attributes(attend: "yes")

or no depending on the type u want.

Also have a look at this for the create/update method in rails.

create_or_update method in rails

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top