Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:30:21+00:00 2026-06-07T03:30:21+00:00

I’m using a custom action to get the id of a project into the

  • 0

I’m using a custom action to get the id of a project into the session, so that only relevant info for that project is shown in other areas. I’ve made a custom action in the projects controller, and am having trouble getting a link to work in the view to call that action. I just get an error saying “Couldn’t find project without ID”. I’m new to rails – I know it’s probably an easy question, but help would be much appreciated, thanks!

View Code:

<%= link_to 'Select Project', :action => :select_project %>

Controller Code:

def select_project
  @project = Project.find(params[:id])
  session[:project_id] = @project.id
end

Routes:

resources :projects do
  collection do
    get :select_project
  end
end

Alternative routes code:

resources :projects do
  put 'select_project', on: :member
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:30:23+00:00Added an answer on June 7, 2026 at 3:30 am

    This is untested but I believe it is what you are looking for:

    Routes:

    resources :projects do
      member do
        post :set_current
      end
    end
    

    this should create the following:

    • Endpoint: /projects/:id/set_current POST
    • Helper: set_current_project_path

    Controller

    def set_current
      project = Project.find(params[:id])
      session[:project_id] = project.id
      redirect_to projects_path, :notice => "Current project set to #{project.name}"
    end
    

    Views

    # index / erb tags excluded for simplicity 
    @projects.each do |project|
      link_to 'Select Project', set_current_project_path(project), :method => :post
    end
    
    # show
    <%= link_to 'Select Project', set_current_project_path(@project), :method => :post %>
    

    See:

    • http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
    • http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

    Note also the use of ‘post’ instead of ‘get’, since we are changing the state of an object (session)
    it is preferred to use a post not a get, otherwise users might pull up an old get request in the address bar
    of their browser and set their session to a project unknowingly.

    like varatis said – use rake routes or CONTROLLER=projects rake routes to help with determining what your route/path helpers look like and what http verbs they are expecting


    And is there a reason why it’s project not @project in the controller

    The @project creates an instance variable; in a rails controller instance variables are made available to the views. This set_current action will never render a view, so no reason to make an instance variable out of it.


    How come you have to set it to member and not collection in the routes

    any action where you want to reference params[:id] should be a member route, an alternative would be to leave it as a collection route and pass params[:project_id] and pass that in all of your link_to calls, but in this case member makes more sense.

    I believe resources :projects is a short cut for this break down

    member do
      get :show
      get :edit
      put :update      
      delete :destroy
    end
    collection do
      get :index
      get :new
      post :create
    end
    

    hopefully that clarifies your questions some?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.