خرید بک لینک

Vote count: 0

I am getting an ActiveModel::ForbiddenAttributesError when trying to create a new task. I think I have eliminated all of the stupid errors, so I would love help figuring it out.

My tasks_controller is as follows:

class TasksController < ApplicationController
  def index
    @tasks = current_user.Task.all
  end

  def create
    @task = Task.new(params[:task])  <<<<<<<<<<<THIS LINE
    @task.user = current_user
    if @task.save
      flash[:notice] = "Your exercise results were saved!"
      redirect_to tasks_path
    else
      render :action => 'new'
    end
  end

  def new
    @task = Task.new
  end

  def edit
    @task = Task.find(params[:id])
  end

  def update
    @task = Task.find(params[:id])
    if @task.update_attributes(params[:task])   <<<<<<<<<<<THIS LINE
      redirect_to tasks_path
  end

  def destroy
    @task = Task.find(params[:id])
    @tasks.destroy
    redirect_to tasks_path
  end

  def show
    @task = Task.find(params[:id])
  end

  private

  def task_params
    params.require(:task).permit(:name, :reps, :weight, :Idea, :user_id)
  end

  end
end

On the two indicated lines I have tried both params[:task] and 'task_params'. The first throws up the ForbiddenAttributesError; the latter throws up undefined local variable or methodtask_params' for #`.

Here's my tasks#new erb:

  <%= form_for @task do |f| %>
  <div class="col-xs-12">
    <%= f.label "Exercise Name" %>
    <%= f.text_field :name, class: "form-control" %>
    </div>
    <div class="col-xs-6">
      <%= f.label "Reps" %>
      <%= f.number_field :reps, class: "form-control" %>
    </div>
    <div class="col-xs-6">
      <%= f.label "Weight" %>
      <%= f.text_field :weight, class: "form-control" %>
    </div>
    <div class="col-xs-12">
      <%= f.label "Comments/Notes" %>
      <%= f.text_field :Idea, class: "form-control" %>
    </div>

    <div class="text-center"><%= f.submit "Record Exercise", class: "btn-primary" %></div>
  <% end %>

Finally, my schema for tasks is as follows, so I know the names of the variables are right:

  create_table "tasks", force: :cascade do |t|
    t.string   "name"
    t.integer  "user_id"
    t.integer  "reps"
    t.integer  "weight"
    t.text     "Idea"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

Can anyone guide me in fixing this "the Ruby way"?

asked 42 secs ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 16 مرداد 1395 ساعت: 11:20

صفحه بندی