Alexander
Alexander

Reputation: 70

Ruby on Rails: Form won't create hidden_field

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?

Upvotes: 0

Views: 164

Answers (1)

Ghar
Ghar

Reputation: 635

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 %>

Upvotes: 2

Related Questions