From: Ravi Dtv on
My partial:


<tr>
<td class="people_profile">
<div class="clear_float_effect">
<div class="profile_block">

<p>
<span title="Click to edit" id="edit_location">

<%= link_to grouprow.group_name %>

</span>
<a href="#" class="short_description_text btn"
id="edit_group_link"> <%= custom_button('Change') %> </a>



</p>
</div>

<div>
<% form_tag (:controller => "users" , :action => "addfriendtogroups")
do %>

Add friend:

<%= text_field_with_auto_complete :user, 'login', {:size => 20},
{:tokens => ','} %>

<%= hidden_field_tag :group_name ,grouprow.group_name %>
<%= submit_tag "Add to group", :class =>
"submit_input_button rounded_corner" %>
<% end %>
</div>

</div>
</td>

</tr>



My controller:

def addfriendtogroups
@f=User.find_by_login(params[:login])
@g=Group.find_by_group_name(params[:group_name])


@addfriend=GroupFriend.create(:user_id => current_user.id,
:group_id => @g.id,:friend_id => @f.id)

respond_to do |format|

if @addfriend.save

flash[:notice] = "Friend added to group successfully."
format.html {redirect_to :back}

end

end
end


I get the following error

RuntimeError in UsersController#addfriendtogroups

Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id

Request

Parameters:

{"group_name"=>"testinggroup2",
"commit"=>"Add to group",
"authenticity_token"=>"MHB8Ys8vccsGwbgnsb9r5T68oqirPpW6YUscfDojZhY=",
"id"=>"testing",
"user"=>{"login"=>"santosh"}}

I need to fetch the id of login (santosh) from users table and id of the
groupname (testinggroup2) from groups and insert the userid, friendid
(id of santosh), groupid (id of testinggroup2) into another table.

Please help. Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Gregor Panek on
hi,

pass your parameters to your partial like this:
<%=render :partial => 'users/grouprow' ,:collection => @groups%>


and when you want to call the parameters in your partial, you have to call it by the name of your partial so it will be something like this:

grouprow.id
grouprow.name


Greg

> I am trying to pass login and group_name from partial to controller, but
> I dont get the values.
>
> I see the following error:
> RuntimeError in UsersController#addfriendtogroups
>
> Called id for nil, which would mistakenly be 4 -- if you really wanted
> the id of nil, use object_id
>
> I tried in the console, where I get the result. But, in application I
> cannot pass the values.
>
>
>
> html page
> -----------------
>
> <% @groups.each do |user| %>
> <%= render :partial => 'users/grouprow', :locals => {:user
> => user}%>
>
> <% end %>
>
> Partial
> -----------
> <tr>
> <td class="people_profile">
> <div class="clear_float_effect">
> <div class="profile_block">
>
> <p>
> <span title="Click to edit" id="edit_location">
>
> <%= link_to user.group_name %>
>
> </span>
> <a href="#" class="short_description_text btn"
> id="edit_group_link"> <%= custom_button('Change') %> </a>
>
>
>
> </p>
> </div>
>
> <div>
> <% form_tag (:controller => "users" , :action => "addfriendtogroups")
> do %>
>
> Add friend:
> <%= text_field_with_auto_complete :user, 'login', {:size =>
> 20}, {:tokens => ','} %>
>
> <%= submit_tag "Add to group", :class =>
> "submit_input_button rounded_corner" %>
> <% end %>
> </div>
>
> </div>
> </td>
>
> </tr>
>
>
>
> Controller
> ----------------
>
> def addfriendtogroups
> @f=User.find_by_login(params[:login]).id
> @g=Group.find_by_group_name(params[:group_name]).id
>
> @addfriend=GroupFriend.create(:user_id => current_user.id ,
> :group_id => @g,:friend_id => @f)
>
> respond_to do |format|
>
> if @addfriend.save
>
> flash[:notice] = "Friend added to group successfully."
> format.html {redirect_to :back}
>
> end
>
> end
> end
> --
> Posted via http://www.ruby-forum.com/.
>


From: Gregor Panek on
I can't see an error in your controller,
the only think i see ist that you got for your username this parameter:
"user"=>{"login"=>"santosh"}

so what you can probably try is to change your input type into this:
<%= text_field_with_auto_complete :user {:size =>
20}, {:tokens => ','} %>

and your controller in this
@f=User.find_by_login(params[:user]).id

i hope that your column login in the Model User is expecting a String with the Name and not an id.




Am 19.05.2010 um 14:24 schrieb Ravi Dtv:

> Gregor Panek wrote:
>> yeah that's because you pass only one parameter to the hidden_field_tag
>> so you got this:
>> <input id="tags_list" name="tags_list" type="hidden" />
>>
>> and your value is still nil, so you have to pass a second parameter to
>> the hidden_field_tag to set the value:
>>
>> <%=hidden_field_tag :group_name, "yourgroupname"%>
>> then you got this in html
>>
>> <input id="token" name="token" type="hidden" value="yourgroupname" />
>> now you should get with params[:group_name] the value that you set
>>
>> hope this works :)
>
> Thanks Gregor Panek. I am able to pass the values but plz check my
> action in the controller.
> RuntimeError in UsersController#addfriendtogroups
>
> Called id for nil, which would mistakenly be 4 -- if you really wanted
> the id of nil, use object_id
>
> Request
>
> Parameters:
>
> {"group_name"=>"testinggroup2",
> "commit"=>"Add to group",
> "authenticity_token"=>"MHB8Ys8vccsGwbgnsb9r5T68oqirPpW6YUscfDojZhY=",
> "id"=>"testing",
> "user"=>{"login"=>"santosh"}}
>
>
> I need to get the id from login( santosh) and id from Group
> (testinggroup2)
> and insert the both ids into another table.
>
>
> Please help. Thank you.
>
> def addfriendtogroups
> @f=User.find_by_login(params[:login]).id
> @g=Group.find_by_group_name(params[:group_name]).id
>
> @addfriend=GroupFriend.create(:user_id => current_user.id ,
> :group_id => @g,:friend_id => @f)
>
> respond_to do |format|
>
> if @addfriend.save
>
> flash[:notice] = "Friend added to group successfully."
> format.html {redirect_to :back}
>
> end
>
> end
> end
>
>
> --
> Posted via http://www.ruby-forum.com/.
>


From: Ravi Dtv on
Gregor Panek wrote:
> yeah that's because you pass only one parameter to the hidden_field_tag
> so you got this:
> <input id="tags_list" name="tags_list" type="hidden" />
>
> and your value is still nil, so you have to pass a second parameter to
> the hidden_field_tag to set the value:
>
> <%=hidden_field_tag :group_name, "yourgroupname"%>
> then you got this in html
>
> <input id="token" name="token" type="hidden" value="yourgroupname" />
> now you should get with params[:group_name] the value that you set
>
> hope this works :)

Thanks Gregor Panek. I am able to pass the values but plz check my
action in the controller.
RuntimeError in UsersController#addfriendtogroups

Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id

Request

Parameters:

{"group_name"=>"testinggroup2",
"commit"=>"Add to group",
"authenticity_token"=>"MHB8Ys8vccsGwbgnsb9r5T68oqirPpW6YUscfDojZhY=",
"id"=>"testing",
"user"=>{"login"=>"santosh"}}


I need to get the id from login( santosh) and id from Group
(testinggroup2)
and insert the both ids into another table.


Please help. Thank you.

def addfriendtogroups
@f=User.find_by_login(params[:login]).id
@g=Group.find_by_group_name(params[:group_name]).id

@addfriend=GroupFriend.create(:user_id => current_user.id ,
:group_id => @g,:friend_id => @f)

respond_to do |format|

if @addfriend.save

flash[:notice] = "Friend added to group successfully."
format.html {redirect_to :back}

end

end
end


--
Posted via http://www.ruby-forum.com/.