0

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"=>""}
1
  • 2
    what is user(params[:id]) and question(params[:id]) ?? Commented Aug 31, 2014 at 16:51

1 Answer 1

2

The error that you're getting is because in the controller you're using the user variable which isn't defined. You want to use the uppercase model name User.

Getting the user id

If this is a logged in user then you can get their id by looking in the session. Usually Rails application will have a helper like this:

def current_user
  @current_user ||= User.find(session[:user_id]) if session[:user_id]
end

You don't need this helper if you're using devise -- it's already done for you.

Getting the question id

First make sure that your associated resources are nested. So in our routes.rb file:

resources :questions do
  resources :answers
end

This means that to create an answer we'll need to POST /questions/:id/answers.

The form changes to:

<%= form_for [@daily, @answer] do |answer| %>
  <%= answer.label :answer, "What do you think?" %>
  <%= answer.text_area :answer %>
  <%= answer.button %>
<% end %>

and in the controller:

def create
  @answer = Answer.new(answer_params)
  @answer.user = current_user
  @answer.question = Question.find(params[:id])

  if @answer.save
    redirect_to root
  else
    render 'new'
  end
end
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for this! It has gotten me one step closer. It is passing the question ID but still isn't locating the user. The error now says: 'ActiveModel::MissingAttributeError in AnswersController#create can't write unknown attribute 'user_id''. I put the def current_user method in the AnswersController. That was correct? Thanks!
Question: how are you handling user login? Do users even log in?
Yes, everything is being handled through devise. At this point it is all open but once I get it functioning I will add restrictions so that users can only perform certain activities while logged in.
If you're using devise then current_user is already defined for you.
@Ahmed I guess there is no user_id attribute in answer. Don't need assign user_id to answer, assign question to answer is enough.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.