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?