UPDATE: It seems like the error was probably due to a devise conflict. I used some content from another app which apparently made devise go nutty and not be able to read user sessions. I'll be starting with a clean install and seeing if I have better luck. Thanks!
I am creating a page which has a single question on it. On that page I have a form where people can answer that question. The form itself just has an answer block and a submit button. However, I want to tie it to both the user submitting the form and the question the answer is linked to. For security I want to do this in the controller, not the view. The answer form is being shown on the questions#index page. I am currently getting the following error:
undefined method `user' for #<AnswersController:0x007fe618e5cc10>
I suspect that if it made it past the user I would get the same for 'question'
The questions#index looks like this:
<div class="home_question">
<h1><%= @daily.question %></h1>
<div class="answer_form">
<%= form_for @answer do |answer| %>
<%= answer.label :answer, "What do you think?" %>
<%= answer.text_area :answer %>
<%= answer.button %>
<% end %>
</div>
</div>
The Questions Controller looks like this:
def index
@daily = Question.find_by(show_month: Time.now.month, show_day: Time.now.day)
@answer = Answer.new
end
The Answers Controller looks like this:
def index
@answers = Answer.all
@answer = Answer.new
end
def new
@answer = Answer.new
end
def create
@answer = Answer.new(answer_params)
@user = user(params[:id])
@question = question(params[:id])
if @answer.save
redirect_to root
else
render 'new'
end
end
private
def answer_params
params.require(:answer).permit(:answer, :users_id, :questions_id)
end
The data currently being passed by the form is:
{"utf8"=>"✓",
"authenticity_token"=>"mA16+dg7+Edzxvu/FVWR5r8PZ9zdNaOyvOwSz1VpOXU=",
"answer"=>{"answer"=>"test"},
"button"=>""}
user(params[:id])andquestion(params[:id])??