0

I'm trying to create a form with a helper in Rails but all my fields aren't created. I have a helper that I include in my view (I'll include them both). But since I'm new to rails I'm not even sure I'm doing this the right way.

When i write it like this it at first looks like it works since the button is created but when i click it no values are passed and an empty row is created in the DB (except for id and timestamp).

users_helper.rb

module UsersHelper

    def sub_button(u)
        @current_user = User.find session[:user_id]
        @temp_user = u
        @sub = Subscription.where("userID = ? AND followingID = ?", @current_user.id, @temp_user.id)
        if @sub.blank?
            @following = false
        else
            @following = true
        end
        if(u.username != @current_user.username)
            if @following
                form_for(:subscription, :url => { :controller => "subscriptions", :action => "unsubscribe" }) do |s|
                    s.hidden_field(:userID, :value => @current_user.id)
                    s.hidden_field(:followingID, :value => u.id)
                    s.submit "Unfollow"
                end
            else
                form_for(:subscription, :url => { :controller => "subscriptions", :action => "subscribe" }) do |s|
                    s.hidden_field(:userID, :value => @current_user.id)
                    s.hidden_field(:followingID, :value => u.id)
                    s.submit "Follow"
                end
            end
        end
    end
end

index.html.erb

<h2>All users</h2>

<table>
    <tr>
        <th>Username</th>
        <th>Email</th>
    </tr>

    <% @user.each do |u| %>
        <tr>
            <td><%= u.username %></td>
            <td><%= u.email %></td>
            <td><%= sub_button(u) %></td>
        </tr>
    <% end %>
</table>

So I was thinking if I'm missing something to create the fields.. any clues?

1 Answer 1

2

I'm not sure, but I think this is how it's supposed to be organized:

module UserHelper
  def sub_button u
    @current_user = User.find session[:user_id]
    @temp_user = u
    @sub = Subscription.where("userID = ? AND followingID = ?", @current_user.id, @temp_user.id)
    if @sub.blank?
        @following = false
    else
        @following = true
    end
    if(u.username != @current_user.username)
        if @following
          render partial: 'shared/unfollow', locals: { current_user: @current_user, u: u }
        else
          render partial: 'shared/follow', locals: { current_user: @current_user, u: u }
        end
    end
end

views/shared/_unfollow.html.erb

<%= form_for(:subscription, url: { controller: "subscriptions", action: "unsubscribe" }) do |s| %>
   <%= s.hidden_field(:userID, value: current_user.id) %>
   <%= s.hidden_field(:followingID, value: u.id) %>
   <%= s.submit "Unfollow" %>
<% end %>

views/shared/_follow.html.erb

<%= form_for(:subscription, url: { controller: "subscriptions", action: "subscribe" }) do |s| %>
  <%= s.hidden_field(:userID, :value => current_user.id) %>
  <%= s.hidden_field(:followingID, :value => u.id) %>
  <%= s.submit "Follow" %>
<% end %>
2
  • Thanks. This is probably a much better way to organize it! But I still have the same problem.. the hidden_fields are not created and I just submit an (almost) empty form by clicking the button..
    – Alexander
    Commented Apr 27, 2013 at 22:36
  • Changed this: <% s.hidden_field(:userID, :value => current_user.id) %> <% s.hidden_field(:followingID, :value => u.id) %> to this: <%= s.hidden_field(:userID, :value => current_user.id) %> <%= s.hidden_field(:followingID, :value => u.id) %> and now it works. Thank you for your help!
    – Alexander
    Commented Apr 27, 2013 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.