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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:06:54+00:00 2026-06-14T17:06:54+00:00

In my Rails 3 application I have a simple model relationship which is as

  • 0

In my Rails 3 application I have a simple model relationship which is as follows:

Animal Model

has_many :claims

Claim Model

belongs_to :animal
has_many :payments

Payments Model

belongs_to :claim

The claim table has a column called animal_id and the payments table has a column called claim_id.

I can successfully list all payments owned by a claim.

What I am trying to do is add a link within the animal show view that creates a new claim with the animal_id already set and the same with the claim show view for the new payment link.

From what I have read you can manually create a link_to with the controller and action set, like so:

Claim Show View

<%= link_to 'Create a Payment', {:controller => "payments", :action => "new", :claim_id => '@claim.id'} %>

This doesn’t seem to work, although it does correctly link to the new payment page.

Is there a simpler way?

  • 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-14T17:06:55+00:00Added an answer on June 14, 2026 at 5:06 pm

    Off the top of my head, and untested, I think the following would work:

    = link_to 'Create Payment', payments_path(:payment=>{:claim_id=>@claim.id}), :method => :post
    

    Adding a hash inside the url helper is just going to put params on the url, which will be interpreted by rails the same way “normal” params posted from a form are.

    This will be equivalent to submitting a new payment form, so it will redirect the same way that would. If you want it to be handled via ajax just add :remote => true, and be sure your the create action on your PaymentsController responds to javascript correctly.


    Update: Read your question again, you want the new payment form with the claim_id already set?

    Ok, there are many ways you could do this. If you pass params to the new action you can take the claim_id from the params, the same as I showed above for creating a record:

    = link_to 'New Payment', new_payment_path(:claim_id=>@claim.id)
    

    This should link you to /payments/new?claim_id=123

    Then in your controller:

    def new
      @payment = Payment.new(:claim_id => params[:claim_id])
    end
    

    However, as was pointed out in the comments, the better way to do this is to nest payments under claims:

    In your routes:

    resources :claims do
      resources :payments
    end
    

    In your PaymentsController:

    before_filter :load_claim
    
    def new
      @payment = @claim.payments.build
    end
    
    def create
      @payment = @claim.payments.build(params[:payment])
      ...
    end
    
    private
    def load_claim
      @claim = Claim.find(params[:claim_id])
    end
    

    In your link:

    = link_to 'New Payment', new_claim_payment_path(@claim)
    

    Just be aware that now your payment path helpers will need claims in them, ie:

    = link_to 'Payment', claim_payment_path(@claim, payment)
    

    Same thing for form helpers:

    = form_for [@claim, @payment] ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple rails application were book belongs_to user and user has_many book.
I have a Rails 3.2.2 application which is a simple company intranet, however although
I have a Rails 3 simple scaffolded application having one model like that: class
I have a simple model for suggestions in my Rails 3 application. I am
I have a simple rails application in which I'm trying to add a very
I have a Rails simple application that has two main models. A person model
I am new to rails and have developed a simple rails application on my
I have a simple rails 2.3.4 application I am trying to get running with
I have created a simple blog application with Ruby on Rails. The applications consists
I have built a very simple blog application using Ruby on Rails. New to

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.