Rails Survey response form









up vote
0
down vote

favorite












I have a survey application that I confused on final side.
here is app models properties:



User

- Survey
user_id

title
- Question
title
survey_id
type: multiple_choice|check_boxes|short_answer
- Option
title
question_id

(Till here is okay. I can create surveys these includes more nested forms) Issue is after created surveys. (users responses)

-Response
user_id
survey_id
-Answer
question_id
response_id
option_id


Creating survey with nested attributes is okay. Problem is at Response side. how should be my response controller and response form on Survey show.html.erb?



There is response controller nested attributes below;



 def response_params
params.require(:response).permit(:id, :user_id, :survey_id, answers_attributes:[:question_id, :response_id, :option_id ] )
end


I should tell that survey can includes multiple questions these with only radio_buttons (independent radio buttons are other issue)



This issue made me so tired. I'll be glad if you can help me. Thanks.
For Source code: Click for source codes



updated files:



Response model:



class Response < ApplicationRecord
belongs_to :user
belongs_to :survey
has_many :answers, dependent: :destroy
validates :survey, presence: true
counter_culture :option
accepts_nested_attributes_for :answers
end


Survey_controller:



def new_response
@survey = Survey.find(params[:id])
@response = @survey.responses.build

# now, the tricky part, you have to build the Answer objects so you can use the nested form later
@survey.questions.each do |q|
@response.answers.build question: q
end
end



def create_response
@survey = Survey.find(params[:id])
@response = @survey.build(response_params)
@response.user = current_user
@response.save
end


Routes:



Rails.application.routes.draw do

devise_for :users
resources :surveys do
member do
get :new_response
get :create_response
end
end
root 'surveys#index'

end


form:



- # surveys/new_response.html.haml
- # You need to define a nested route inside survey resource to create the response
= form_for @response, url: create_response_survey_path(@survey) do |f|
- # you can iterate over all the answers already initialized
= f.fields_for :answers do |ff|
- # get the question from the current answer to show the title and options and a hidden_field with the question id
- q = ff.object.question

= q.title
= ff.hidden_field :question_id

- # add the radios for each options for the question
- q.options.each do |option|
= label_tag do
= ff.radio_button :option_id, option.id
= option.title

= f.submit 'Send'









share|improve this question



























    up vote
    0
    down vote

    favorite












    I have a survey application that I confused on final side.
    here is app models properties:



    User

    - Survey
    user_id

    title
    - Question
    title
    survey_id
    type: multiple_choice|check_boxes|short_answer
    - Option
    title
    question_id

    (Till here is okay. I can create surveys these includes more nested forms) Issue is after created surveys. (users responses)

    -Response
    user_id
    survey_id
    -Answer
    question_id
    response_id
    option_id


    Creating survey with nested attributes is okay. Problem is at Response side. how should be my response controller and response form on Survey show.html.erb?



    There is response controller nested attributes below;



     def response_params
    params.require(:response).permit(:id, :user_id, :survey_id, answers_attributes:[:question_id, :response_id, :option_id ] )
    end


    I should tell that survey can includes multiple questions these with only radio_buttons (independent radio buttons are other issue)



    This issue made me so tired. I'll be glad if you can help me. Thanks.
    For Source code: Click for source codes



    updated files:



    Response model:



    class Response < ApplicationRecord
    belongs_to :user
    belongs_to :survey
    has_many :answers, dependent: :destroy
    validates :survey, presence: true
    counter_culture :option
    accepts_nested_attributes_for :answers
    end


    Survey_controller:



    def new_response
    @survey = Survey.find(params[:id])
    @response = @survey.responses.build

    # now, the tricky part, you have to build the Answer objects so you can use the nested form later
    @survey.questions.each do |q|
    @response.answers.build question: q
    end
    end



    def create_response
    @survey = Survey.find(params[:id])
    @response = @survey.build(response_params)
    @response.user = current_user
    @response.save
    end


    Routes:



    Rails.application.routes.draw do

    devise_for :users
    resources :surveys do
    member do
    get :new_response
    get :create_response
    end
    end
    root 'surveys#index'

    end


    form:



    - # surveys/new_response.html.haml
    - # You need to define a nested route inside survey resource to create the response
    = form_for @response, url: create_response_survey_path(@survey) do |f|
    - # you can iterate over all the answers already initialized
    = f.fields_for :answers do |ff|
    - # get the question from the current answer to show the title and options and a hidden_field with the question id
    - q = ff.object.question

    = q.title
    = ff.hidden_field :question_id

    - # add the radios for each options for the question
    - q.options.each do |option|
    = label_tag do
    = ff.radio_button :option_id, option.id
    = option.title

    = f.submit 'Send'









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a survey application that I confused on final side.
      here is app models properties:



      User

      - Survey
      user_id

      title
      - Question
      title
      survey_id
      type: multiple_choice|check_boxes|short_answer
      - Option
      title
      question_id

      (Till here is okay. I can create surveys these includes more nested forms) Issue is after created surveys. (users responses)

      -Response
      user_id
      survey_id
      -Answer
      question_id
      response_id
      option_id


      Creating survey with nested attributes is okay. Problem is at Response side. how should be my response controller and response form on Survey show.html.erb?



      There is response controller nested attributes below;



       def response_params
      params.require(:response).permit(:id, :user_id, :survey_id, answers_attributes:[:question_id, :response_id, :option_id ] )
      end


      I should tell that survey can includes multiple questions these with only radio_buttons (independent radio buttons are other issue)



      This issue made me so tired. I'll be glad if you can help me. Thanks.
      For Source code: Click for source codes



      updated files:



      Response model:



      class Response < ApplicationRecord
      belongs_to :user
      belongs_to :survey
      has_many :answers, dependent: :destroy
      validates :survey, presence: true
      counter_culture :option
      accepts_nested_attributes_for :answers
      end


      Survey_controller:



      def new_response
      @survey = Survey.find(params[:id])
      @response = @survey.responses.build

      # now, the tricky part, you have to build the Answer objects so you can use the nested form later
      @survey.questions.each do |q|
      @response.answers.build question: q
      end
      end



      def create_response
      @survey = Survey.find(params[:id])
      @response = @survey.build(response_params)
      @response.user = current_user
      @response.save
      end


      Routes:



      Rails.application.routes.draw do

      devise_for :users
      resources :surveys do
      member do
      get :new_response
      get :create_response
      end
      end
      root 'surveys#index'

      end


      form:



      - # surveys/new_response.html.haml
      - # You need to define a nested route inside survey resource to create the response
      = form_for @response, url: create_response_survey_path(@survey) do |f|
      - # you can iterate over all the answers already initialized
      = f.fields_for :answers do |ff|
      - # get the question from the current answer to show the title and options and a hidden_field with the question id
      - q = ff.object.question

      = q.title
      = ff.hidden_field :question_id

      - # add the radios for each options for the question
      - q.options.each do |option|
      = label_tag do
      = ff.radio_button :option_id, option.id
      = option.title

      = f.submit 'Send'









      share|improve this question















      I have a survey application that I confused on final side.
      here is app models properties:



      User

      - Survey
      user_id

      title
      - Question
      title
      survey_id
      type: multiple_choice|check_boxes|short_answer
      - Option
      title
      question_id

      (Till here is okay. I can create surveys these includes more nested forms) Issue is after created surveys. (users responses)

      -Response
      user_id
      survey_id
      -Answer
      question_id
      response_id
      option_id


      Creating survey with nested attributes is okay. Problem is at Response side. how should be my response controller and response form on Survey show.html.erb?



      There is response controller nested attributes below;



       def response_params
      params.require(:response).permit(:id, :user_id, :survey_id, answers_attributes:[:question_id, :response_id, :option_id ] )
      end


      I should tell that survey can includes multiple questions these with only radio_buttons (independent radio buttons are other issue)



      This issue made me so tired. I'll be glad if you can help me. Thanks.
      For Source code: Click for source codes



      updated files:



      Response model:



      class Response < ApplicationRecord
      belongs_to :user
      belongs_to :survey
      has_many :answers, dependent: :destroy
      validates :survey, presence: true
      counter_culture :option
      accepts_nested_attributes_for :answers
      end


      Survey_controller:



      def new_response
      @survey = Survey.find(params[:id])
      @response = @survey.responses.build

      # now, the tricky part, you have to build the Answer objects so you can use the nested form later
      @survey.questions.each do |q|
      @response.answers.build question: q
      end
      end



      def create_response
      @survey = Survey.find(params[:id])
      @response = @survey.build(response_params)
      @response.user = current_user
      @response.save
      end


      Routes:



      Rails.application.routes.draw do

      devise_for :users
      resources :surveys do
      member do
      get :new_response
      get :create_response
      end
      end
      root 'surveys#index'

      end


      form:



      - # surveys/new_response.html.haml
      - # You need to define a nested route inside survey resource to create the response
      = form_for @response, url: create_response_survey_path(@survey) do |f|
      - # you can iterate over all the answers already initialized
      = f.fields_for :answers do |ff|
      - # get the question from the current answer to show the title and options and a hidden_field with the question id
      - q = ff.object.question

      = q.title
      = ff.hidden_field :question_id

      - # add the radios for each options for the question
      - q.options.each do |option|
      = label_tag do
      = ff.radio_button :option_id, option.id
      = option.title

      = f.submit 'Send'






      ruby-on-rails ruby






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 14:02

























      asked Nov 10 at 16:54









      mihraç cerrahoğlu

      115




      115






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          I wouldn't use the Survey's show action to show the form to create a Response, I think it's better to approach it as a new_response action to make it cleaner and leave the show action just to show the actual survey (not to respond it). Something like:



          class SurveysController < ApplicationController
          def new_response
          @survey = Survey.find(params[:id])
          @response = @survey.responses.build

          # now, the tricky part, you have to build the Answer objects so you can use the nested form later
          @survey.questions.each do |q|
          @response.anwers.build question: q
          end
          end


          Now, you can have a form for the response:



          - # surveys/new_response.html.haml
          - # You need to define a nested route inside survey resource to create the response
          = form_for @response, url: create_response_survey_path(@survey) do |f|
          - # you can iterate over all the answers already initialized
          = f.fields_for :answers do |ff|
          - # get the question from the current answer to show the title and options and a hidden_field with the question id
          - q = ff.object.question

          = q.title
          = ff.hidden_field :question_id

          - # add the radios for each options for the question
          - q.options.each do |option|
          = label_tag do
          = ff.radio_button :choice_id, option.id
          = option.title

          = f.submit 'Send'


          Your response_params should be something like:



          def response_params
          params.require(:response).permit(answers_attributes: [:question_id, :choice_id])
          end


          note that I removed the :survey_id and the :user_id, you don't want a user to hack your form, change a survey_id or user_id and add responses to another survey made by another user!



          and your create_response action:



          def create_response
          @survey = Survey.find(params[:id])
          @response = @survey.build(response_params)
          @response.user = current_user
          @response.save
          end


          Hope it makes sense.






          share|improve this answer




















          • Thanks Brother. I am so glad because of you spend time for creating that answer. I will try and notify you. Other question is adding user_id to params. I added because i need users who responded to survey. Also some users(i.e: moderator) can create survey and i need their user_id. But it will be recorded automatically by your solution. Am i true?
            – mihraç cerrahoğlu
            Nov 10 at 20:14










          • I'm setting it explicittly using @response.user = current_user. I don't think it's a good idea to make it a parameter fo the request since it can be changed by a malicious user. You should already have the current user loaded on the session, no need to make it a parameter of the request.
            – arieljuod
            Nov 10 at 21:26










          • also i added to routes like; get 'surveys/new_response' => "surveys#new_response" and path is working but path result is like : ` localhost:3000/surveys/new_response.7` than i added also get method under surveys. result: undefined method responses' for nil:NilClass` @response = @survey.responses.build (under new_response method in surveys_controller)
            – mihraç cerrahoğlu
            Nov 11 at 12:35










          • till the new_response.html.haml everythings are seems okay. I set routes and find parameters than it is okay. issue is on form. render is ` - q = ff.object.question` and undefined method question' for nil:NilClass
            – mihraç cerrahoğlu
            Nov 11 at 14:09











          • I'm not sure what's your current route, but I gues you have some resources :survey, right? add a block with member do; get :new_response; end, it will create a route with url surveys/:id/new_response, check the rails routing guide for more info on that. The @response = @survey.response.build fails because you had the wrong parameters and the controller can't find the survey.
            – arieljuod
            Nov 11 at 16:02










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241227%2frails-survey-response-form%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          I wouldn't use the Survey's show action to show the form to create a Response, I think it's better to approach it as a new_response action to make it cleaner and leave the show action just to show the actual survey (not to respond it). Something like:



          class SurveysController < ApplicationController
          def new_response
          @survey = Survey.find(params[:id])
          @response = @survey.responses.build

          # now, the tricky part, you have to build the Answer objects so you can use the nested form later
          @survey.questions.each do |q|
          @response.anwers.build question: q
          end
          end


          Now, you can have a form for the response:



          - # surveys/new_response.html.haml
          - # You need to define a nested route inside survey resource to create the response
          = form_for @response, url: create_response_survey_path(@survey) do |f|
          - # you can iterate over all the answers already initialized
          = f.fields_for :answers do |ff|
          - # get the question from the current answer to show the title and options and a hidden_field with the question id
          - q = ff.object.question

          = q.title
          = ff.hidden_field :question_id

          - # add the radios for each options for the question
          - q.options.each do |option|
          = label_tag do
          = ff.radio_button :choice_id, option.id
          = option.title

          = f.submit 'Send'


          Your response_params should be something like:



          def response_params
          params.require(:response).permit(answers_attributes: [:question_id, :choice_id])
          end


          note that I removed the :survey_id and the :user_id, you don't want a user to hack your form, change a survey_id or user_id and add responses to another survey made by another user!



          and your create_response action:



          def create_response
          @survey = Survey.find(params[:id])
          @response = @survey.build(response_params)
          @response.user = current_user
          @response.save
          end


          Hope it makes sense.






          share|improve this answer




















          • Thanks Brother. I am so glad because of you spend time for creating that answer. I will try and notify you. Other question is adding user_id to params. I added because i need users who responded to survey. Also some users(i.e: moderator) can create survey and i need their user_id. But it will be recorded automatically by your solution. Am i true?
            – mihraç cerrahoğlu
            Nov 10 at 20:14










          • I'm setting it explicittly using @response.user = current_user. I don't think it's a good idea to make it a parameter fo the request since it can be changed by a malicious user. You should already have the current user loaded on the session, no need to make it a parameter of the request.
            – arieljuod
            Nov 10 at 21:26










          • also i added to routes like; get 'surveys/new_response' => "surveys#new_response" and path is working but path result is like : ` localhost:3000/surveys/new_response.7` than i added also get method under surveys. result: undefined method responses' for nil:NilClass` @response = @survey.responses.build (under new_response method in surveys_controller)
            – mihraç cerrahoğlu
            Nov 11 at 12:35










          • till the new_response.html.haml everythings are seems okay. I set routes and find parameters than it is okay. issue is on form. render is ` - q = ff.object.question` and undefined method question' for nil:NilClass
            – mihraç cerrahoğlu
            Nov 11 at 14:09











          • I'm not sure what's your current route, but I gues you have some resources :survey, right? add a block with member do; get :new_response; end, it will create a route with url surveys/:id/new_response, check the rails routing guide for more info on that. The @response = @survey.response.build fails because you had the wrong parameters and the controller can't find the survey.
            – arieljuod
            Nov 11 at 16:02














          up vote
          0
          down vote



          accepted










          I wouldn't use the Survey's show action to show the form to create a Response, I think it's better to approach it as a new_response action to make it cleaner and leave the show action just to show the actual survey (not to respond it). Something like:



          class SurveysController < ApplicationController
          def new_response
          @survey = Survey.find(params[:id])
          @response = @survey.responses.build

          # now, the tricky part, you have to build the Answer objects so you can use the nested form later
          @survey.questions.each do |q|
          @response.anwers.build question: q
          end
          end


          Now, you can have a form for the response:



          - # surveys/new_response.html.haml
          - # You need to define a nested route inside survey resource to create the response
          = form_for @response, url: create_response_survey_path(@survey) do |f|
          - # you can iterate over all the answers already initialized
          = f.fields_for :answers do |ff|
          - # get the question from the current answer to show the title and options and a hidden_field with the question id
          - q = ff.object.question

          = q.title
          = ff.hidden_field :question_id

          - # add the radios for each options for the question
          - q.options.each do |option|
          = label_tag do
          = ff.radio_button :choice_id, option.id
          = option.title

          = f.submit 'Send'


          Your response_params should be something like:



          def response_params
          params.require(:response).permit(answers_attributes: [:question_id, :choice_id])
          end


          note that I removed the :survey_id and the :user_id, you don't want a user to hack your form, change a survey_id or user_id and add responses to another survey made by another user!



          and your create_response action:



          def create_response
          @survey = Survey.find(params[:id])
          @response = @survey.build(response_params)
          @response.user = current_user
          @response.save
          end


          Hope it makes sense.






          share|improve this answer




















          • Thanks Brother. I am so glad because of you spend time for creating that answer. I will try and notify you. Other question is adding user_id to params. I added because i need users who responded to survey. Also some users(i.e: moderator) can create survey and i need their user_id. But it will be recorded automatically by your solution. Am i true?
            – mihraç cerrahoğlu
            Nov 10 at 20:14










          • I'm setting it explicittly using @response.user = current_user. I don't think it's a good idea to make it a parameter fo the request since it can be changed by a malicious user. You should already have the current user loaded on the session, no need to make it a parameter of the request.
            – arieljuod
            Nov 10 at 21:26










          • also i added to routes like; get 'surveys/new_response' => "surveys#new_response" and path is working but path result is like : ` localhost:3000/surveys/new_response.7` than i added also get method under surveys. result: undefined method responses' for nil:NilClass` @response = @survey.responses.build (under new_response method in surveys_controller)
            – mihraç cerrahoğlu
            Nov 11 at 12:35










          • till the new_response.html.haml everythings are seems okay. I set routes and find parameters than it is okay. issue is on form. render is ` - q = ff.object.question` and undefined method question' for nil:NilClass
            – mihraç cerrahoğlu
            Nov 11 at 14:09











          • I'm not sure what's your current route, but I gues you have some resources :survey, right? add a block with member do; get :new_response; end, it will create a route with url surveys/:id/new_response, check the rails routing guide for more info on that. The @response = @survey.response.build fails because you had the wrong parameters and the controller can't find the survey.
            – arieljuod
            Nov 11 at 16:02












          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          I wouldn't use the Survey's show action to show the form to create a Response, I think it's better to approach it as a new_response action to make it cleaner and leave the show action just to show the actual survey (not to respond it). Something like:



          class SurveysController < ApplicationController
          def new_response
          @survey = Survey.find(params[:id])
          @response = @survey.responses.build

          # now, the tricky part, you have to build the Answer objects so you can use the nested form later
          @survey.questions.each do |q|
          @response.anwers.build question: q
          end
          end


          Now, you can have a form for the response:



          - # surveys/new_response.html.haml
          - # You need to define a nested route inside survey resource to create the response
          = form_for @response, url: create_response_survey_path(@survey) do |f|
          - # you can iterate over all the answers already initialized
          = f.fields_for :answers do |ff|
          - # get the question from the current answer to show the title and options and a hidden_field with the question id
          - q = ff.object.question

          = q.title
          = ff.hidden_field :question_id

          - # add the radios for each options for the question
          - q.options.each do |option|
          = label_tag do
          = ff.radio_button :choice_id, option.id
          = option.title

          = f.submit 'Send'


          Your response_params should be something like:



          def response_params
          params.require(:response).permit(answers_attributes: [:question_id, :choice_id])
          end


          note that I removed the :survey_id and the :user_id, you don't want a user to hack your form, change a survey_id or user_id and add responses to another survey made by another user!



          and your create_response action:



          def create_response
          @survey = Survey.find(params[:id])
          @response = @survey.build(response_params)
          @response.user = current_user
          @response.save
          end


          Hope it makes sense.






          share|improve this answer












          I wouldn't use the Survey's show action to show the form to create a Response, I think it's better to approach it as a new_response action to make it cleaner and leave the show action just to show the actual survey (not to respond it). Something like:



          class SurveysController < ApplicationController
          def new_response
          @survey = Survey.find(params[:id])
          @response = @survey.responses.build

          # now, the tricky part, you have to build the Answer objects so you can use the nested form later
          @survey.questions.each do |q|
          @response.anwers.build question: q
          end
          end


          Now, you can have a form for the response:



          - # surveys/new_response.html.haml
          - # You need to define a nested route inside survey resource to create the response
          = form_for @response, url: create_response_survey_path(@survey) do |f|
          - # you can iterate over all the answers already initialized
          = f.fields_for :answers do |ff|
          - # get the question from the current answer to show the title and options and a hidden_field with the question id
          - q = ff.object.question

          = q.title
          = ff.hidden_field :question_id

          - # add the radios for each options for the question
          - q.options.each do |option|
          = label_tag do
          = ff.radio_button :choice_id, option.id
          = option.title

          = f.submit 'Send'


          Your response_params should be something like:



          def response_params
          params.require(:response).permit(answers_attributes: [:question_id, :choice_id])
          end


          note that I removed the :survey_id and the :user_id, you don't want a user to hack your form, change a survey_id or user_id and add responses to another survey made by another user!



          and your create_response action:



          def create_response
          @survey = Survey.find(params[:id])
          @response = @survey.build(response_params)
          @response.user = current_user
          @response.save
          end


          Hope it makes sense.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 17:52









          arieljuod

          5,63711121




          5,63711121











          • Thanks Brother. I am so glad because of you spend time for creating that answer. I will try and notify you. Other question is adding user_id to params. I added because i need users who responded to survey. Also some users(i.e: moderator) can create survey and i need their user_id. But it will be recorded automatically by your solution. Am i true?
            – mihraç cerrahoğlu
            Nov 10 at 20:14










          • I'm setting it explicittly using @response.user = current_user. I don't think it's a good idea to make it a parameter fo the request since it can be changed by a malicious user. You should already have the current user loaded on the session, no need to make it a parameter of the request.
            – arieljuod
            Nov 10 at 21:26










          • also i added to routes like; get 'surveys/new_response' => "surveys#new_response" and path is working but path result is like : ` localhost:3000/surveys/new_response.7` than i added also get method under surveys. result: undefined method responses' for nil:NilClass` @response = @survey.responses.build (under new_response method in surveys_controller)
            – mihraç cerrahoğlu
            Nov 11 at 12:35










          • till the new_response.html.haml everythings are seems okay. I set routes and find parameters than it is okay. issue is on form. render is ` - q = ff.object.question` and undefined method question' for nil:NilClass
            – mihraç cerrahoğlu
            Nov 11 at 14:09











          • I'm not sure what's your current route, but I gues you have some resources :survey, right? add a block with member do; get :new_response; end, it will create a route with url surveys/:id/new_response, check the rails routing guide for more info on that. The @response = @survey.response.build fails because you had the wrong parameters and the controller can't find the survey.
            – arieljuod
            Nov 11 at 16:02
















          • Thanks Brother. I am so glad because of you spend time for creating that answer. I will try and notify you. Other question is adding user_id to params. I added because i need users who responded to survey. Also some users(i.e: moderator) can create survey and i need their user_id. But it will be recorded automatically by your solution. Am i true?
            – mihraç cerrahoğlu
            Nov 10 at 20:14










          • I'm setting it explicittly using @response.user = current_user. I don't think it's a good idea to make it a parameter fo the request since it can be changed by a malicious user. You should already have the current user loaded on the session, no need to make it a parameter of the request.
            – arieljuod
            Nov 10 at 21:26










          • also i added to routes like; get 'surveys/new_response' => "surveys#new_response" and path is working but path result is like : ` localhost:3000/surveys/new_response.7` than i added also get method under surveys. result: undefined method responses' for nil:NilClass` @response = @survey.responses.build (under new_response method in surveys_controller)
            – mihraç cerrahoğlu
            Nov 11 at 12:35










          • till the new_response.html.haml everythings are seems okay. I set routes and find parameters than it is okay. issue is on form. render is ` - q = ff.object.question` and undefined method question' for nil:NilClass
            – mihraç cerrahoğlu
            Nov 11 at 14:09











          • I'm not sure what's your current route, but I gues you have some resources :survey, right? add a block with member do; get :new_response; end, it will create a route with url surveys/:id/new_response, check the rails routing guide for more info on that. The @response = @survey.response.build fails because you had the wrong parameters and the controller can't find the survey.
            – arieljuod
            Nov 11 at 16:02















          Thanks Brother. I am so glad because of you spend time for creating that answer. I will try and notify you. Other question is adding user_id to params. I added because i need users who responded to survey. Also some users(i.e: moderator) can create survey and i need their user_id. But it will be recorded automatically by your solution. Am i true?
          – mihraç cerrahoğlu
          Nov 10 at 20:14




          Thanks Brother. I am so glad because of you spend time for creating that answer. I will try and notify you. Other question is adding user_id to params. I added because i need users who responded to survey. Also some users(i.e: moderator) can create survey and i need their user_id. But it will be recorded automatically by your solution. Am i true?
          – mihraç cerrahoğlu
          Nov 10 at 20:14












          I'm setting it explicittly using @response.user = current_user. I don't think it's a good idea to make it a parameter fo the request since it can be changed by a malicious user. You should already have the current user loaded on the session, no need to make it a parameter of the request.
          – arieljuod
          Nov 10 at 21:26




          I'm setting it explicittly using @response.user = current_user. I don't think it's a good idea to make it a parameter fo the request since it can be changed by a malicious user. You should already have the current user loaded on the session, no need to make it a parameter of the request.
          – arieljuod
          Nov 10 at 21:26












          also i added to routes like; get 'surveys/new_response' => "surveys#new_response" and path is working but path result is like : ` localhost:3000/surveys/new_response.7` than i added also get method under surveys. result: undefined method responses' for nil:NilClass` @response = @survey.responses.build (under new_response method in surveys_controller)
          – mihraç cerrahoğlu
          Nov 11 at 12:35




          also i added to routes like; get 'surveys/new_response' => "surveys#new_response" and path is working but path result is like : ` localhost:3000/surveys/new_response.7` than i added also get method under surveys. result: undefined method responses' for nil:NilClass` @response = @survey.responses.build (under new_response method in surveys_controller)
          – mihraç cerrahoğlu
          Nov 11 at 12:35












          till the new_response.html.haml everythings are seems okay. I set routes and find parameters than it is okay. issue is on form. render is ` - q = ff.object.question` and undefined method question' for nil:NilClass
          – mihraç cerrahoğlu
          Nov 11 at 14:09





          till the new_response.html.haml everythings are seems okay. I set routes and find parameters than it is okay. issue is on form. render is ` - q = ff.object.question` and undefined method question' for nil:NilClass
          – mihraç cerrahoğlu
          Nov 11 at 14:09













          I'm not sure what's your current route, but I gues you have some resources :survey, right? add a block with member do; get :new_response; end, it will create a route with url surveys/:id/new_response, check the rails routing guide for more info on that. The @response = @survey.response.build fails because you had the wrong parameters and the controller can't find the survey.
          – arieljuod
          Nov 11 at 16:02




          I'm not sure what's your current route, but I gues you have some resources :survey, right? add a block with member do; get :new_response; end, it will create a route with url surveys/:id/new_response, check the rails routing guide for more info on that. The @response = @survey.response.build fails because you had the wrong parameters and the controller can't find the survey.
          – arieljuod
          Nov 11 at 16:02

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241227%2frails-survey-response-form%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3