0

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 !

Denys Medynskyi
  • 2,353
  • 8
  • 39
  • 70

2 Answers2

1

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 %>
VitalyP
  • 1,867
  • 6
  • 22
  • 31
1

As I understand your Question, a User gets payed for their Websites, depending on attributes of the website. I would structure this as follows:

  • create a method in the Website model to compute this particular website's "value"
  • create a method in user summing up the values for all websites in user.website e.g. with

    total = user.websites.inject{ |sum,website| sum+website.value }

  • use this last method to get the amount in your controller action. It should not be computed in the controller, much less in the view according to the MVC pattern.

Speaking of that, I would consider creating a special Controller for the pay action (rather than stuffing it into StaticPagesController, it's not a static page after all), maybe even a Model without Persistence to handle the compute_payment method which I feel does not belong in User.rb.

bento
  • 2,079
  • 1
  • 16
  • 13