Rails 4 - paths -


i trying figure out rails 4.

i have project model , project_question model.

project question belongs project.

i want form redirects project page after question submitted. keep getting directed list of projects, rather specific project in respect of question has been asked. struggling figure out how learn rails.

in project_question_form have:

<div class="containerfluid">   <div class="row">     <div class="col-md-10 col-md-offset-1">        <div class="header-project">what know?</div>        <%= render 'form' %>       <div class="formminor">       <%= link_to 'back', projects_path(@project) %>       </div>     </div>   </div> </div> 

i expect bracketed bit in "back" link go project page user on when clicked 'ask question'. instead, user taken page lists index of projects.

in ask question form, have:

<div class="containerfluid">   <div class="row">     <div class="col-md-10 col-md-offset-1">       <%= simple_form_for :project_questions |f| %>               <%= f.input :project_id, as: :hidden, input_html: {value: @project_question_id} %>               <%= f.input :title, label: 'question:',  :label_html => {:class => 'question-title'}, placeholder: 'type question here', :input_html => {:style => 'width: 100%', :rows => 4, class: 'response-project'} %>               <%= f.input :content, label: 'is there context or other information?', :label_html => {:class => 'question-title'}, placeholder: 'context might answer question', :input_html => {:style => 'width: 100%', :rows => 5, class: 'response-project'} %>            <br><br><br>               <%= f.button :submit, 'send!', :class => "cpb" %>      <% end %>     </div>   </div> </div> 

in routes.rb, have:

resources :projects     resources :project_questions       resources :project_answers     end   end 

can see have done wrong? have tried using (@project_id) , (@project.id) in place of (@project) in first form. none of alternatives work.

my projects_controller has:

class projectscontroller < applicationcontroller   #layout :projects_student_layout    before_action :authenticate_user!      # /projects   # /projects.json   def index     @projects = current_user.projects       #can have more 1 index? need change in routes? if want list related projects , expiring projects - how do within 1 index?      #is there way order these, educators in order of course , students in order of next milestone date?     #@projects.order("created_at desc")   end    # def index2  #    @projects = project.find_xxx_xx  #  end    def list     @projects = project.find(:all)   end    def toggle_draft     @project = project.find(params[:id])     @project.draft = true     @project.save     redirect_to project_path(@project)   end    # /projects/1   # /projects/1.json   def show     #authorise @project      @project = project.find(params[:id])     @creator = user.find(@project.creator_id)     @creator_profile = @creator.profile      #@approver_profile = user.find(@project.educator_id).profile #educators people approve projects #    if profile == 'studnet'     #@approval = @project.approval   #   @invitations = @project.project_invitations    end    # /projects/new   def new     #authorise @project     @project = project.new     @project.scope = scope.new     @project.scope.datum = datum.new     @project.scope.material = material.new     @project.scope.mentoring = mentoring.new     @project.scope.participant = participant.new     @project.scope.funding = funding.new     @project.scope.ethic = ethic.new     @project.scope.group_research = groupresearch.new     @project.scope.backgroundip = backgroundip.new     @project.scope.result = result.new     @project.scope.finalise = finalise.new    end    # /projects/1/edit   def edit     #authorise @project     @project =project.find(params[:id])       end    # post /projects   # post /projects.json   def create     #authorise @project     @project = project.new(project_params)     @project.creator_id = current_user.id     @project.users << current_user     respond_to |format|       if @project.save         format.html { redirect_to @project }, flash[:notice] = "project saved"         format.json { render action: 'show', status: :created, location: @project }       else         format.html { render action: 'new' }         format.json { render json: @project.errors, status: :unprocessable_entity }       end     end   end    # patch/put /projects/1   # patch/put /projects/1.json   def update     #authorise @project     @project = project.find(params[:id])     @project.creator_id = current_user.id      respond_to |format|       if @project.update(project_params)         format.html { redirect_to @project }         format.json { head :no_content }       else         format.html { render action: 'edit' }         format.json { render json: @project.errors, status: :unprocessable_entity }       end     end   end    # delete /projects/1   # delete /projects/1.json   def destroy     #authorise @project      @project.destroy     respond_to |format|       format.html { redirect_to projects_url }       format.json { head :no_content }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_project       @project = project.find(params[:id])     end      # never trust parameters scary internet, allow white list through.      def project_params       params.require(:project).permit(       :id, :title, :description, :video_proposal, :link_to_video_proposal,       :expiry_date_for_sponsor_interest,  :motivation, :approach,       :completion_date, :start_date, :industry_id,  :recurring_project,       :frequency, :date_for_student_invitation, :date_for_student_interest, :closed, :student_objective,        :industry_relevance, :hero_image, :project_id,       project_question_attributes: [:title, :content, :user_id, :project_id,       project_answer_attributes: [:answer, :project_question_id]],       scope_attributes: [:id, :project_id, :data, :material, :mentoring, :participant, :funding, :ethic, :group, :result, :disclosure, :finalise,                          :if_mentoring, :if_participant, :if_funding, :if_ethic, :if_group_research, :if_backgroundip, :if_datum, :if_material,                          datum_attributes: [:id, :prim_sec, :qual_quant, :survey, :survey_link, :experiment, :other_type, :other_description,                                            :confidential, :data_description, :scope_id],                          material_attributes: [:id, :mattype, :description, :scope_id],                          mentoring_attributes: [:id, :frequency, :description, :scope_id],                          funding_attributes: [:id,  :expenses, :honorarium, :financing, :currency, :size, :amount_expenses, :amount_honorarium,                                  :comment, :amount_principal_financing, :return_on_finance, :period_of_return, :expense_description, :amount_expenses_pennies, :amount_honorarium_pennies, :amount_principal_financing_pennies,                                                :amount_expenses_currency, :scope_id],                          participant_attributes: [:id,  :title, :description, :location, :costs, :participation_cost,                                                    :eligibility, :eligibility_criteria, :currency, :participation_cost_pennies, :participation_cost_currency,                                                    :location_specific ],                          group_research_attributes: [:id, :number_of_group_members, :scope_id],                          ethic_attributes: [:id, :obtained, :date_expected, :ethics_comment, :ethics_policy_link, :scope_id],                          result_attributes: [:id, :report, :standard_licence, :bespoke_licence, :option, :assignment, :other_outcome,                                              :consulting, :link_to_bespoke_licence, :description],                          disclosure_attributes: [:id, :allusers, :publicity, :limitedorganisation, :limitedindustry, :limiteduser, :approveddisclosure],                          backgroundip_attributes: [:id, :scope_id, :copyright, :design, :patent, :trademark, :geographical_indication,                                                    :trade_secret, :other, :identifier_copyright, :identifier_design, :identifier_patent,                                                    :identifier_trademark, :identifier_geographical_indication, :identifier_trade_secret,                                                    :identifier_other, :description, :registered_owner, :unregistered_interest, :conditions,                                                    :pbr, :identifier_pbr ],                           finalise_attributes: [:id, :draft, :reminder, :reminder_date, :finalised_at, :scope_id]                           ]       )       end 

when bundle exec rake routes (as suggested below), lot of routes models. ones include project_question are:

project_answers#destroy                     project_project_questions      /projects/:project_id/project_questions(.:format)                                               project_questions#index                                               post     /projects/:project_id/project_questions(.:format)                                               project_questions#create                  new_project_project_question      /projects/:project_id/project_questions/new(.:format)                                           project_questions#new                 edit_project_project_question      /projects/:project_id/project_questions/:id/edit(.:format)                                      project_questions#edit                      project_project_question      /projects/:project_id/project_questions/:id(.:format)                                           project_questions#show                                               patch    /projects/:project_id/project_questions/:id(.:format)                                           project_questions#update                                               put      /projects/:project_id/project_questions/:id(.:format)                                           project_questions#update                                               delete   /projects/:project_id/project_questions/:id(.:format)                                           project_questions#destroy 

chad's route suggestion:

projects      /projects(.:format)                                                                             projects#index                                               post     /projects(.:format)                                                                             projects#create                                   new_project      /projects/new(.:format)                                                                         projects#new                                  edit_project      /projects/:id/edit(.:format)                                                                    projects#edit                                       project      /projects/:id(.:format)                                                                         projects#show                                               patch    /projects/:id(.:format)                                                                         projects#update                                               put      /projects/:id(.:format)                                                                         projects#update                                               delete   /projects/:id(.:format)                                                                         projects#destroy 

my project_question controller:

class projectquestionscontroller < applicationcontroller   before_action :set_project_question, only: [:show, :edit, :update, :destroy]    # /project_questions   # /project_questions.json   def index     @project_questions = projectquestion.all   end    # /project_questions/1   # /project_questions/1.json   def show   end    # /project_questions/new   def new     @project_question = projectquestion.new     @project_id = params[:project_id]     @project_question.project_answers[0] = projectanswer.new    end    # /project_questions/1/edit   def edit   end    # post /project_questions   # post /project_questions.json   def create     @project_question = projectquestion.new(project_question_params)     @project_question.project_id = project_question_params[:project_id]       respond_to |format|       if @project_question.save         format.html { redirect_to @project_question, notice: 'project question created.' }         format.json { render action: 'show', status: :created, location: @project_question }       else         format.html { render action: 'new' }         format.json { render json: @project_question.errors, status: :unprocessable_entity }       end     end   end    # patch/put /project_questions/1   # patch/put /project_questions/1.json   def update     respond_to |format|       if @project_question.update(project_question_params)         format.html { redirect_to @project_question, notice: 'project question updated.' }         format.json { head :no_content }       else         format.html { render action: 'edit' }         format.json { render json: @project_question.errors, status: :unprocessable_entity }       end     end   end    # delete /project_questions/1   # delete /project_questions/1.json   def destroy     @project_question.destroy     respond_to |format|       format.html { redirect_to project_questions_url }       format.json { head :no_content }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_project_question       @project_question = projectquestion.find(params[:id])     end      # never trust parameters scary internet, allow white list through.     def project_question_params       params[:project_question].permit(:id, :title, :content, :project_id, :user_id,       project_answer_atttibutes: [:id, :answer, :project_question_id, :user_id]       )     end end 

this link in projects show page:

  <%= link_to 'ask question', new_project_project_question_path(:project_id => @project.id) %> <% end %> 

first, try:

project_path(@project_id) 

then, if doesn't work, need make sure when go project page ask question page, you're passing @project.id parameter called project_id.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -