简体   繁体   中英

Rails - passing session info in hidden fields - getting error

I'm trying to pass a project id from a session in a hidden field on a task form, so that when the task is created, it has the id of the project that it is assigned to. I've done this before fine, and have even tried copying over the code that I used from when it worked, but changing names and I'm getting errors no matter what I do - if anyone could help point out where I'm going wrong, it would be much appreciated, thanks!

The error I'm getting with this configuration is: "unknown attribute: project_id"

View Code (tasks/_form):

<%= form_for(@task) do |f| %>
  <div class="field">
  <%= f.hidden_field :project_id, :value => session[:project_id] %>
  </div>
...
<% end %>

Model Code (task):

attr_accessible :project_id

belongs_to :project

Controller code (tasks_controller):

def new
  @task = Task.new
  @project_id = session[:project_id]

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

def create
  project_id = session[:project_id]
  @task = Task.new(params[:task])

  respond_to do |format|
    if @task.save
      format.html { redirect_to @task, notice: 'Task was successfully created.' }
      format.json { render json: @task, status: :created, location: @task }
    else
      format.html { render action: "new" }
      format.json { render json: @task.errors, status: :unprocessable_entity }
    end
  end
end

Here's the application trace - it is pointing to line 46, which in my code is the '@task = Task.new(params[:task])' line in the create action...?

app/controllers/tasks_controller.rb:46:in `new'
app/controllers/tasks_controller.rb:46:in `create'

任务模型是否具有project_id列?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM