adding categories for posts rails - ruby-on-rails

I'm trying to add catagories to my posts with a has_and_belongs_to_many relationship
I"m pretty close but just a little off.
here is my post index.html.erb
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Post Index<small> by title</small></h1>
</div>
</div>
<%= form_tag posts_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<center><%= link_to 'Create New Post', new_post_path %></center>
</p>
<p><% #posts.sort_by(&:comments_count).reverse.each do |post| %></p>
<p><%= image_tag avatar_url(post.user), size: "31x31" %> <%= post.user.name %></p>
<p><strong>Title: </strong><%= post.title %></p>
<p><strong>Summary: </strong><%= post.summary %></p>
<p><%= post.catagories.name %>
<p><%= link_to 'Read Full Post', post %> Total Comments for post . . . (<%= post.comments.count %>)</p>
<p><strong>Posted ON: </strong><%= post.created_at.strftime('%b %d, %Y') %></p>
<br>
<p><% if current_user.admin? %><%= link_to "delete", post, method: :delete, data: { confirm: "You sure" } %>
<% end %>
<% if current_user.admin? %><%= link_to 'Edit', edit_post_path(post) %><% end %></p>
<% end %>
</div>
catagory.rb file
class Catagory < ActiveRecord::Base
has_and_belongs_to_many :posts
end
post.rb file
class Post < ActiveRecord::Base
belongs_to :user
has_many :comments
has_and_belongs_to_many :catagories
mount_uploader :image, ImageUploader
searchable do
text :title, :boost => 5
text :content
end
def comments_count
comments.count
end
end
post _form.html.erb
<%= form_for #post, :html => {:multipart => true} do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :summary %><br>
<%= f.text_field :summary %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<% Catagory.all.each do |catagory| %>
<%= check_box_tag catagory.id %>
<%= catagory.name %><br/>
<% end %><br>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>
right now in the new it just says Catagory in stead of one of the three catagories I set in my rails console in my post index.html.erb file
#<ActiveRecord::Relation [#<Catagory id: 1, name: "lolcats", created_at: "2013-11-20 15:17:05", updated_at: "2013-11-20 15:17:05">, #<Catagory id: 2, name: "surfing", created_at: "2013-11-20 15:17:42", updated_at: "2013-11-20 15:17:42">, #<Catagory id: 3, name: "dance", created_at: "2013-11-20 15:18:11", updated_at: "2013-11-20 15:18:11">]>
the check box comes up fine in my post _form though, anyone feel like taking a stab at it?

according to this rails cast you need to change check_box_tag here
<% Catagory.all.each do |catagory| %>
<%= check_box_tag catagory.id %>
<%= catagory.name %><br/>
<% end %><br>
to
<%= check_box_tag "post[catagory_ids][]", catagory.id, #post.catagories.include?(catagory) %> #why catAgory, by the way?

Related

Why is post title not showing up?

So here it is:
For some reason this is not showing the title for my post.
However when I press show option and it goes to post's page you can see al the detail.
_form.html.erb
<%= form_with(model: post, local: true) do |form| %>
<% if post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date, id: :post_date %>
</div>
<div class="field">
<%= form.label :name %>
<%= form.text_area :name, id: :post_name %>
</div>
<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id, id: :post_user_id, value: current_user.id %>
</div>
<div class="field">
<%= form.label :description %>
<%= form.text_area :description, id: :post_description %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
#show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Date:</strong>
<%= #post.date %>
</p>
<p>
<strong>Name:</strong>
<%= #post.name %>
</p>
<p>
<strong>User:</strong>
<%= #post.user_id %>
</p>
<p>
<strong>Description:</strong>
<%= #post.description %>
</p>
<% if current_user == #post.user %>
<%= link_to 'Edit', edit_post_path(#post) %> |
<%end%>
<%= link_to 'Back', posts_path %>
<h1>Editing Post</h1>
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
edit.html.erb
<%= render 'form', post: #post %>
<%= link_to 'Show', #post %> |
<%= link_to 'Back', posts_path %>
I've tried changing it <%= form.text_area :name, id: :post_name %>
to string_area :name and also string_field but none of it is working.
In fact when I change it to the last 2, I get a method error if I go on the edit page. I'm still fairly new to rails and to ruby so I would greatly appreciate some help. I did try to google however and was unsuccessful, I guess I didn't know what to search for or maybe I worded it wrong.

How to get model attributes to save in order?

I'm building a quiz app, to create a quiz I do this:
def new
#quiz = Quiz.new
50.times do
question = #quiz.questions.build
5.times { question.answers.build }
end
end
enter code here
Which uses nested parameters. The problem is, sometimes the quiz doesn't save in order (such as when I create a new quiz OR I update an existing quiz). If I create a quiz in particular order, say Question 1, Question 2, Question 3, I want the questions in that quiz to remain in the same order after I update or create it. Right now, if I update a quiz the order gets jumbled up if I want to show the quiz using something like quiz.questions.each do |question|
I wasn't aware that ruby databases don't enforce order, so how do I make sure that if I create a quiz, the order in which I enter the questions (top to bottom) will always be the order that the questions are presented in?
If you are interested, this is the form I use to create the quiz:
<%= form_for(#quiz) do |f| %>
<% if #quiz.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#quiz.errors.count, "error") %> prohibited this quiz from being saved:</h2>
<ul>
<% #quiz.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class = 'field'>
<%= f.label :difficulty, "Difficulty of Quiz, 1 to 3 with 3 being most difficult" %>
<%= f.text_field :difficulty %>
</div>
<div class="field">
<%= f.label :for_unsubscribed, "Check to have this quiz be visible to logged in but unsubscribed users" %>
<%= f.check_box :for_unsubscribed %>
</div>
<%= f.fields_for :questions do |question_attribute| %>
<div class = 'inner-c'>
<p>
<%= question_attribute.label :content, "Question" %> <span><b><%= question_attribute.index + 1 %></b></span> <br/>
<%= question_attribute.text_area :content, :cols => 100, :rows => 4 %>
</p>
<p>
<%= question_attribute.label :explanation, "Answer Explanation" %> <br/>
<%= question_attribute.text_area :explanation, :cols => 100, :rows => 6 %>
</p>
<%= question_attribute.label :_destroy, "Remove Question"%>
<%= question_attribute.check_box :_destroy %><br/>
<%= question_attribute.label :passage, "Reference Passage" %> <br/>
<%= question_attribute.text_area :passage, :rows => 3, :class => 'passage-input' %>
<%#= question_attribute.label :question_explanation, "Question Explanation" %>
<%#= question_attribute.text_area :question_explanation, :rows => 10 %>
</p>
<%= question_attribute.fields_for :answers do |answer_attribute| %>
<p>
<%= answer_attribute.label :content, "Answer" %>
<%= answer_attribute.text_field :content %>
<%= answer_attribute.label :correct_answer, "Check to indicate correct answer", :class => 'inline' %>
<%= answer_attribute.check_box :correct_answer, :class => 'inline'%>
</p>
<% end %>
</div> <!-- inner-c -->
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Rails - Couldn't find Student with 'id'=

I'm getting the error above when I try to create a a 'mark' for a 'student'. I can't figure out how to pass the :student_id when I create a new mark.
Routes
Rails.application.routes.draw do
resources :students do
resources :marks
end
resources :marks
root 'students#index'
Marks Controller
class MarksController < ApplicationController
def create
#student = Student.find(params[:student_id])
#mark = #student.marks.create(params[:input1, :input2, :input3, :weight1, :weight2, :weight3, :mark1, :mark2, :mark3, :final_mark].permit(:input1, :input2, :input3, :weight1, :weight2, :weight3, :mark1, :mark2, :mark3, :final_mark))
#mark.save
if #mark.save
redirect_to student_path(#student)
else
render 'new'
end
end
def new
#mark = Mark.new
end
private
def set_mark
#mark = Mark.find(params[:id])
end
end
Students Show View
<p id="notice"><%= notice %></p>
<p>
<strong>Student Number</strong>
<%= #student.StudentNumber %>
</p>
<p>
<strong>Project Title</strong>
<%= #student.ProjectTitle %>
</p>
<p>
<strong>Project PDF</strong>
<%= #student.ProjectTitle %>
</p>
<p>
<strong>Reader 1</strong>
<%= #student.Reader1 %>
</p>
<p>
<strong>Reader 2</strong>
<%= #student.Reader2 %>
</p>
<h3> <%= link_to 'Add Mark', new_student_mark_path(#student), class:"btn btn-warning"%> </h3>
<p>
<strong>Reader 3</strong>
<%= #student.Reader3 %>
</p>
<%= link_to 'Edit', edit_student_path(#student) %> |
<%= link_to 'Back', students_path %>
Marks Form
<%= form_for #mark, html: {multipart: true} do |f| %>
<% if #mark.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#mark.errors.count, "error") %> prohibited this grading from being saved:</h2>
<ul>
<% #mark.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label 'Introduction' %><br>
<%= f.text_area :input1 %>
<%= f.number_field :weight1 %>
<%= f.number_field :mark1 %>
</div>
<div class="field">
<%= f.label 'Main' %><br>
<%= f.text_area :input2 %>
<%= f.number_field :weight2 %>
<%= f.number_field :mark2 %>
</div>
<div class="field">
<%= f.label 'Conclusion' %><br>
<%= f.text_area :input3 %>
<%= f.number_field :weight3 %>
<%= f.number_field :mark3 %>
</div>
<div class="actions">
<%= f.submit class:"btn-xs btn-success"%>
</div>
<% end %>
Mark model
class Mark < ActiveRecord::Base
belongs_to :student
end
Student Model
class Student < ActiveRecord::Base
has_many :marks
has_attached_file :document
validates_attachment :document, :content_type => { :content_type => %w(application/pdf) }
end
It's probably something really stupid but if anyone could explain the problem I'd be really grateful.
Thanks
I don't suggest you using hidden fields for this purpose.
You should pass student together with mark into form_for helper and rails will generate proper url for you which will look like: /students/:student_id/marks
In this case it will be possible to extract student_id from params in your action later.
form_for [#student, #mark], html: {multipart: true} do |f|
More information about nested resources:
http://guides.rubyonrails.org/routing.html#nested-resources
http://www.informit.com/articles/article.aspx?p=1671632&seqNum=7
https://gist.github.com/jhjguxin/3074080
UPDATE:
Forgot to mention that in order to make this work you need to pass student instance into your template at new action:
def new
#student = Student.find(params[:student_id])
#mark = #student.marks.build
end

Date is not working in rails application with mongo id

I have create an rails application with mongoid but im facing one problem at DATE
I created an scaffold with a name "posting"
When im editing date it will updated....
I follow the instruction of Railscast #238 Mongoid here
there is my posting.rb file
class Posting
include Mongoid::Document
field :title
field :description
field :comments
field :published, :type => Date
end
this my _from.html.erb
<%= form_for(#posting) do |f| %>
<% if #posting.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#posting.errors.count, "error") %> prohibited this posting from being saved:</h2>
<ul>
<% #posting.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_field :description %>
</div>
<div class="field">
<%= f.label :published %><br>
<%= f.date_select :published %>
</div>
<div class="field">
<%= f.label :comments %><br>
<%= f.text_area :comments %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
and finally my show.html.erb file
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= #posting.title %>
</p>
<p>
<strong>Description:</strong>
<%= #posting.description %>
</p>
<p>
<strong>published:</strong>
<%= #posting.published %>
</p>
<p>
<strong>Comments:</strong>
<%= #posting.comments %>
</p>
<%= link_to 'Edit', edit_posting_path(#posting) %> |
<%= link_to 'Back', postings_path %>
What do you mean by not working? doesn't look like you have used published property in any of your views.
in your show.html.erb you are using
<%= f.date_select :pub %>
and in your show.html.erb you are using
<%= #posting.pub %>
However there is no property called pub in your Posting model. What you have there is called published
field :published, :type => Date
You either need to rename it in the model, or in the views to match.

Nested form using paperclip

I have a model called posts, and it has many attachments.
The attachments model is using paperclip.
I made a standalone model for creating attachments which works just fine, this is the view as instructed here (https://github.com/thoughtbot/paperclip):
<% form_for :attachment, #attachment, :url => #attachment, :html => { :multipart => true } do |form| %>
<%= form.file_field :pclip %>
<%= form.submit %>
<% end %>
The nested form in posts looks like this:
<% #attachment = #posts.attachments.build %>
<%= form_for(#posts) do |f| %>
<% if #posts.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#posts.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #posts.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.fields_for :attachments, #attachment, :url => #attachment, :html => { :multipart => true } do |at_form| %>
<%= at_form.file_field :pclip %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
An attachment record is created, but its empty. The file is not uploaded. The post meanwhile, is successfully created...
Any ideas?
You are missing the :multipart option in your form definition:
<% #attachment = #post.attachments.build %>
<%= form_for #post, :html => { :multipart => true } do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.fields_for :attachments, #attachment do |at_form| %>
<%= at_form.file_field :pclip %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Also, your #posts variable should really be #post (single ActiveRecord instance as opposed to an array of ActiveRecord instances).

Resources