Search code examples
ruby-on-railsviewcontrollers

Can't manage to organise function for associated models


I'm thinking about solving my problem and need some help.

I need to pay users, so I have page, where admin see all users and can pay each of them.

I have websites model, which is belong to user. Depends on some attribute from website model I should pay to user, but user has many websites and it is not clear to me how to organise this.

I can get all user's websites by calling

    user.websites

EDIT: I can't get how I can generate for each website separate link, because I din't need to sum all payments - I need SEPARATE link for each website

Here is my pay function(left only related code):

   def pay
     ...
    user = User.find(params[:user_id])
    @params_id = params[:user_id]
    @websites = user.websites
       data = {"actionType" => "PAY",
               "receiverList.receiver(0).email"=> user.email,
              "receiverList.receiver(0).amount" => website.amount,         
    ...

   end

or I should add website each do in my view. something like:

     <% @users.each do |user| %>
      <%@websites.each do |website|%>
     <li>
     <%= user.email %>
      <%= link_to "Pay", pay_path(user.id, website.amount)%>
    </li>
    <%end%>
 <% end %>

What you can reccomend me and I will appreciate any help !


Solution

  • I am not sure I understand your question right but I think this could help you

    <% users.each do |user| %>
      <% user.websites.each do |website| %>
        <span id="user-email"><%= user.email %></span>
        <span id="payment"><%= link_to "Pay #{number_to_currency(website.amount)}", pay_path(user.id, website.id), remote: true, confirm: "Are you sure you want to pay?" %></span>
      <% end %>
    <% end %>