Classified in Computers

Written at on English with a size of 2.18 KB.

rails g model category name:string

rake db:migrate

rails g migration add_category_id_to_jobs category_id:integer

rake db:migrate

Then we associate category and jobs

Then go to article form.Html:

= simple_form_for(@job, html: {class: 'form-horizontal'}) do |f|

=f.Collection_select :category_id, Category.All, :id, :name, {prompt: "Select Category"}, input_html: {class: "dropdown-toggle"}

=f.input :title, label: "Job Titile", input_html: {class: "form-control"}

=f.Input :description, label: "Description", input_html: {class: "form-control"}

=f.Input :company, label: "Your Company", input_html: {class: "form-control"}

=f.Input :url, label: "Link To Job", input_html: {class: "form-control"}

=f.Button :submit, class: "btn btn-primary"

go to jobs controller:

def index

    if params[:category].Blank?

  @jobs = Job.All.Order("created_at DESC")


     @category_id = Category.Find_by(name: params[:category]).Id

     @jobs = Job.Where(category_id: @category_id).Order("created_at DESC")


params.Require(:job).Permit(:title, :description, :company, :url, :category_id)

Entradas relacionadas: