У меня есть табличный тест с полями «user_id» и «post_id». user_id Я извлекаю current_user.id и post_id из таблицы сообщений. Для этого я делаю: В тестовом контроллере: -Получите идентификатор другой таблицы, рельсы
def create
@user = current_user
@test = Test.new(params[:test])
@post = Post.find(params[:id])
@test.post_id = @post.id
@test.user_id = @user.id
respond_to do |format|
@test.save
format.html { redirect_to(:back, :notice => "successfull") }
else
format.html { render :action => "new" }
end
end
В моей модели я создаю ассоциации: -
В test.rb:
belongs_to :post
В post.rb:
has_many :test
Страница просмотра:
= form_for @test, :validate => true do |f|
.span
= f.text_field :email, :placeholder =>"Email Address"
.span
= f.label :name
= f.text_field :name
.span
= f.submit "Save"
user_id сохранить должным образом, но для POST_ID он дает ошибку:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Как я могу решить эту проблему?
Вы уверены, что вы должны получить доступ к 'create' метод только тогда, когда это логин, т.е. 'current_user' не должен быть nil – Salil