Reputation: 335
I feel like this should be an easy thing to figure out, but I'm stumped.
I have a value in a Project's instance variable called ID. I want to pass that value to a new Photos page to associate each photo that is created with that specific project, but I don't want the Project's ID to show up in the visible query string.
I've tried using link_to and button_to, but (I suspect) since I'm using "resources :photos" in my routes, all of the requests that come to photo#new are being interpreted as GET instead of POST.
Helllllllllllllllp!
Thanks to anyone that can give me some insight, I'v been killing myself over this for the past hour or two already.
--Mark
Upvotes: 1
Views: 2631
Reputation: 8400
The usual way to do this in Rails is to create a route that matches urls like this: /projects/4/photos/new
. Doing something else is up to you, but Rails makes it really easy to do stuff like this. See more on routes in Rails 3.
Your entry in routes.rb
should look something like this:
resources :projects do
resources :photos
end
Then in app/controllers/photos_controller.rb
you'd have this for the "New Photo" form page:
def new
@project = Project.find_by_id(params[:project_id])
end
and this for the action that the form in app/views/photos/new.html.erb
submits to:
def create
@project = Project.find_by_id(params[:project_id])
@photo = @project.photos.create(params[:photo])
end
Of course you'll want to have error handling and validation in here, but this is the gist of it. And remember, use GET
for idempotent (non state-changing) actions (e.g. GET /projects/4/photos
), POST
for creating a new thing (e.g. POST /projects/4/photos
), and PUT
for updating an existing thing (e.g. PUT /projects/4/photos/8
).
Upvotes: 1