I learn Rails by writing simple TODO assignments. Two models:
class List < ActiveRecord::Base has_many :tasks, :dependent => :destroy # ... end class Task < ActiveRecord::Base belongs_to :list # ... end
Tasks are routed as embedded resources in the list. Therefore, when a new task is created by the user, a POST message is sent to /lists/:list_id/tasks . While in the view form Tasks#new there is
f.hidden_field :list_id, :value => params[:list_id]
but this is a terrible decision, because anyone can change the meaning of this hidden field.
What is the agreement here? Should I put something like
@task.list_id = params[:list_id]
in Tasks#create action and get rid of the hidden field, or maybe
@task = List.find(params[:list_id]).tasks.new(params[:task]) if @task.save
or is there a better way that I don't know about?
Edit:
Yes, there was a similar question well , and his answer pretty much touched my question. If you have another, send it.
ruby-on-rails
Tomasz Cudziลo
source share