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 4029394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:20:23+00:00 2026-05-20T11:20:23+00:00

It’s been almost a week since I’ve began to dig deeper in forms ,

  • 0

It’s been almost a week since I’ve began to dig deeper in forms , associations , hashes , symbols… But it seems I cannot solve the puzzle without your help .

I am working on a project for displaying different galleries content . The basic idea is when the user sees the names of galleries (names are links ) to be able to click on chosen one. Then all the images ,that belong to this gallery , are displayed . On the bottom there should be a link “Add image in this gallery” .

My models :

 class Gallery < ActiveRecord::Base
attr_accessible  :name
has_many :pictures 
 end


class Picture < ActiveRecord::Base 
attr_accessible  :image 
belongs_to :gallery
 end

I have created index on gallery_id for the ‘pictures’ table .

My big problem appears here , how to pass the gallery_id to the controller’s action ‘new’ . As I’ve seen in “Agile web development with Rails” it could be :
<%= link_to ‘Add a picture here…’,new_picture_path(:gallery_id=>@gallery.id) %>

As it seems in this case the foreign_key :gallery_id is exposed in the URL bar of the browser . The second problem is that :gallery_id is available for the controller ‘new’ function , but “disappears” for the ‘create’ function (causing an error ” Couldn’t find Gallery without an ID “) .
The problem is gone when I add a hidden field in the _form for pictures , in my case :

<%= form_for(@picture)   do |f| %>
<div class="field"> 
      <%= f.hidden_field :gallery_id , :value=>params[:gallery_id] %>
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>

<div class="actions">
<%= f.submit "Create" %>
</div>
<% end %>

Here are my definitions in the ‘pictures’ controller :

def new
@gallery=Gallery.find(params[:gallery_id])
@picture=@gallery.pictures.build 
 end


def create
   @gallery = Gallery.find(params[:gallery_id])  
   @picture = @gallery.pictures.new(params[:picture])
  if @picture.save
     redirect_to(@picture, :notice => 'Picture was successfully created.')
  else    
     redirect_to(galleries ,:notice => 'Picture was NOT created.')
  end

 end

And finaly the link_to definition in show.html.erb for galleries:

<% for picture in selpics(@gallery) %>
 <div id= "thumb" > 
 <%= image_tag picture.image %>
 </div>
<% end %>

 <%= link_to 'Add a picture here...',new_picture_path(:gallery_id=>@gallery.id) %>

Here is the debug output before submitting the image :
— !map:ActiveSupport::HashWithIndifferentAccess
gallery_id: “6”
action: new
controller: pictures

and after submitting the ‘create’ button (with exception raised ) :

{"utf8"=>"✓",
 "authenticity_token"=>"IGI4MfDgbavBShO7R2PXIiK8fGjkgHDPbI117tcfxmc=",
 "picture"=>{"image"=>"wilsonblx.png"},
 "commit"=>"Create"}

As you see , there is nothing like “gallery_id” in the “pictures” hash .

Summarizing my questions to you :

  1. Is there a way to pass the foreign_key without hidden_field ?

  2. Could I hide somehow passing the foreign key form showing in the URL bar ?

  3. Is there an alternative on passing arguments using ‘link_to’ ?

Thank you .

  • 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-05-20T11:20:24+00:00Added an answer on May 20, 2026 at 11:20 am

    You may want to consider reading the Rails Guide on nested resources:

    http://guides.rubyonrails.org/routing.html#nested-resources

    In a nutshell:

    routes.rb

    resources :galleries do
      resources :pictures do
    end
    # Generates the routes: /galleries/:gallery_id/pictures
    

    pictures_controller.rb

    def new
      @gallery = Gallery.find(params[:gallery_id])
      @picture = Picture.new
    end
    def create
      @gallery = Gallery.find(params[:gallery_id]) # gallery_id is passed in the URL
      @picture = @gallery.build(params[:picture])
      if @picture.save
      # success
      else
      # fail
      end
    end
    

    pictures/new.html.erb

    <%= form_for [@gallery, @picture] do |f| %>
      <div class="field"> 
        <%= f.hidden_field :gallery_id , :value=>params[:gallery_id] %>
        <%= f.label :image %><br />
        <%= f.file_field :image %>
      </div>
    
      <div class="actions">
        <%= f.submit "Create" %>
      </div>
    
    <% end %>
    

    Ok, so the gallery_id is still passed through the URL, but I don’t really see anything wrong with that. You have to pass it somewhere, right? You really only have 3 sane choices on where to pass it: a hidden field, as a querystring parameter, or tucked away inside the URL (nested resource). Of the 3, the latter is IMHO the cleanest method.

    If you want to make things even easier on yourself, I highly recommend looking into Jose Valim’s Inherited Resources gem that takes care of a lot of this boilerplate nastiness for you:

    https://github.com/josevalim/inherited_resources

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I

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.