Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Rails 4 Strong Parameters

Strong Parameters Update action

  • Please log in to reply
2 replies to this topic

#1 waggonerdr

waggonerdr

    Passenger

  • Members
  • 2 posts

Posted 28 June 2014 - 05:51 PM

I've been playing with Rails for a while and every time I feel like I'm getting the hang of it, they come out with a new version and depreciate half of the old version. Arghhh!!! Anyway, here's the problem I've been working on for the last few days...

 

I have task and completed_task models:

 

  class Task < ActiveRecord::Base

    belongs_to :user

    has_many :completed_tasks   

  end

 

  class CompletedTask < ActiveRecord::Base

    belongs_to :task 

  end

 

I have a form that says:

 

  <% @tasks.each do |task| %>

    <td><%= link_to task.description, task_path(task)  %></td>

        <td><%= task.user.first_name %></td>

        <td><%= task.value %></td>

        <td><%= task.task_type %></td>

        <td><%= task.frequency %></td>

        <td><%= task.active %></td>

 

        <td><%= task.due_by %></td>

    <%= button_to "Task Completed", new_completed_task_path(:completed =>[:task_id => task.id, :task_value => task.value}) %>

   <%end%>

 

In my completed_task_controller, I have:

 

  def new

    @completed_task = CompletedTask.new(params[:completed]) 

  end

 

  def create

    @completed_task = CompletedTask.new(completed_task_params)

  end

 

When I click on the button to complete a task, I want it to create a record in the completed_tasks table but the params from the parent table are not flowing from the new action to the create action.  I'm guessing it has to do with the strong parameters which I have set as:

 

private

    def set_completed_task

      @completed_task = CompletedTask.find(params[:id])

    end

 

    def completed_task_params

     params.require(:completed_task).permit(:task_id, :task_value)    

    end

 

Any ideas???



#2 waggonerdr

waggonerdr

    Passenger

  • Members
  • 2 posts

Posted 28 June 2014 - 05:57 PM

Also… Here is the error I am getting:

 

ActiveModel::ForbiddenAttributesError

Extracted source (around line #19):
 
def new
  @completed_task = CompletedTask.new(params[:completed])
end


#3 palytoxin

palytoxin

    Signalman

  • Members
  • 10 posts

Posted 29 June 2014 - 02:46 PM

i guess you need to setting params
 
like this: 
 
private
    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params[:user].delete :admin unless current_user.try(:admin?)
      params.require(:user).permit(:email, :password, :password_confirmation, :admin)
    end

 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users