Im new with ruby on rails and I'm again blocked on something really similar as the last time.. I have a User table, a Course table and a Mark table. I can create a Mark with a user_id, a course_id and a grade.
In my models i have :
/* Mark model */
class Mark < ActiveRecord::Base
belongs_to :user
belongs_to :course
end
/* Course model */
class Course < ActiveRecord::Base
has_many :marks
end
/* User model */
class User < ActiveRecord::Base
has_many :marks
end
So what I want to do is when I created a new mark, with my form I want that my form disappear and display a button destroy ! Like this :
<% if @mark.course_ids.include?(@course.id) and @mark.user_ids.include?(user.id)
# Button destroy
<% else %>
<%= form_for Mark.new do |f| %>
<%= hidden_field_tag :course_id, @course.id %>
<%= hidden_field_tag :user_id, user.id %>
Grade: <%= number_field_tag :grade, nil, min: 0, max: 100 %>
<%= f.submit %>
<% end %>
<% end %>
The goal is to avoid the form when the mark just be added previously.. But here obviously @mark.course_ids and @mark.user_ids doesn't exist !!
Thanks for you help !
@markcontainscourse_idanduser_id. So you may writeif @mark.course_id == @course.id && @mark.user_id == user.id@markby making a findall wherecourse_id: params[:id]! This is to get every@markin my table link to this course. So@markis a table and can't call methoduser_idorcourse_idwithout a.each!