jeudi 16 juillet 2015

Where will i write require params in rails 3.2.13?

Todoscontroller.rb

    class TodosController < ApplicationController
  # GET /todos
  # GET /todos.json
  def index
    @todos = Todo.all
    @project = Project.new
    @projects = Project.all

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @todos }

    end
  end

  # GET /todos/1
  # GET /todos/1.json
  def show
    @todo = Todo.find(params[:id])
    @projects = Project.all
    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @todo }
    end
  end

  # GET /todos/new
  # GET /todos/new.json
  def new
    @todo = Todo.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @todo }
    end
  end

  # GET /todos/1/edit
  def edit
    @todo = Todo.find(params[:id])
  end

  # POST /todos
  # POST /todos.json
  def create
    @todo = Todo.new(params[:todo])

    respond_to do |format|
      if @todo.save
        format.html { redirect_to(@todo, :notice => 'Todo was successfully created.') }
        format.json  { render :json => @todo, :status => :created, :location => @todo }
      else
        format.html { render :action => "new" }
        format.json  { render :json => @todo.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /todos/1
  # PUT /todos/1.json
  def update
    @todo = Todo.find(params[:id])

    respond_to do |format|
      if @todo.update_attributes(params[:todo])
        format.html { redirect_to(@todo, :notice => 'Todo was successfully updated.') }
        format.json  { render :json => {} }
      else
        format.html { render :action => "edit" }
        format.json  { render :json => @todo.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /todos/1
  # DELETE /todos/1.json
  def destroy
    @todo = Todo.find(params[:id])
    @todo.destroy

    respond_to do |format|
      format.html { redirect_to(todos_url) }
      format.json  { render :json => {} }
    end
  end
  def newproject
       @projects = Project.all       
    end

end

Todos.rb

 class CreateTodos < ActiveRecord::Migration
  def self.up
    create_table :todos do |t|
      t.string :content
      t.integer :order
      t.boolean :done

      t.timestamps
    end
  end

  def self.down
    drop_table :todos
  end
end

migration file.rb

class AddColumnToTodos < ActiveRecord::Migration
  def self.up
    add_column :todos, :project_id, :integer
  end
   def self.down
    add_column :todos, :project_id
  end
end

todo.rb

    class Todo < ActiveRecord::Base
validates :content, :presence => true
  attr_accessible :content, :order, :done, :project_id
  belongs_to :project  
end

Here i created new field ("project_id") for Todo model. Here my problem project_id is not saved in Database. Where will i write require params in Rails 3.2.13.

Aucun commentaire:

Enregistrer un commentaire