Topic: render :partial => problem

Hi everyone.

I have a remote_function call, that passes an ID to the server (more exactly: to the corresponding controller action). There, an object is created on the basis of that ID. Fine up to here. At the end of the action I'm just saying

Re: render :partial => problem

Your post is truncated, please send it again.

Re: render :partial => problem

Oops, how could that happen?

Hi everyone.

I have a remote_function call, that passes an ID to the server (more exactly: to the corresponding controller action). There, an object is created on the basis of that ID. Fine up to here. At the end of the action I'm just saying render :partial => 'editCustomer' to get response to the client.

What I need to know now is how do I get specific attributes of my object (wich has just been created) into textfields within my partial?

Isn't it corrent to say <%= f.text_field :name %> to assign those values into my view as a result of rails' naming conventions automatically?

Thanks for every helpful answer!

Re: render :partial => problem

First thing to realize is that the form_for context doesn't travel along with the remote request.

If you want partials rendered from AJAX calls, they have to be free of the form_for context

One approach

Create a partial _customer.

<%= text_field_tag customer , :name %>
<%= text_field_tag customer , :address %>

Then in say your new view

<% form_for @customer do | f | %>
   <%= render :partial=>"customer", :object=>@customer %>
   <%= f.submit_tag "Create"
<%

Now in your remote function you can do this

def my_ajax
  @customer = Customer.find(params[:id])
  @customer.address = '12345 West St.'
  render :partial=>"customer", :object=>@customer
end

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: render :partial => problem

Ah, thank you very much. That's new for me.

So, in other words I have to take the form_for out of the partial and put the partial into my form_for instead?
If I do, how do I access my object in the 'new' partial, when there's nothing (no form_for anymore) that refers to it?

Edit:
Now I did, what you said and it works up to the point, that the textfields are showing me the names of the attributes. Whats still wrong?
Here's some code:

Function Call:

remote_function(  :url => { :action => 'editCustomer', 
                            :controller => 'customers'},
                            :with => "'id=' +$('specificCustomers').value");%>

In my Action after having created the object:

render :partial=>"customer", :object=>@customer

And my form_for

<% form_for :customer, @customer, :url => { :controller => "customers", :action => "editCustomer", :remote=> true } do |f| %>
  <%= render :partial=>"customers/customer", :object=>@customer %>
  <%= f.submit 'Save' %>
<% end %>

And my partial

<div>
  <%= text_field_tag customer, :name %>
  <%= text_field_tag customer, :kurz %>
  <%= text_field_tag customer, :info %>
  <%= text_field_tag customer, :strasse %>
  <%= text_field_tag customer, :plz %>
  <%= text_field_tag customer, :ort %>
  <%= text_field_tag customer, :tel %>
  <%= text_field_tag customer, :fax %>
  <%= text_field_tag customer, :mail %>
  <%= text_field_tag customer, :www%>
</div>

And inside the textfields the names of the respective attribute is displayed.
I really really appreciate and will be thankful, If I solve this with your help!

Greets from Berlin smile

Last edited by Ulfmann (2011-02-11 06:55:55)

Re: render :partial => problem

<%= text_field_tag :name, customer.name %>

Sorry I probably flubbed my post.

Berlin, cool, been there, 1 afternoon in 1993, drank a beer and ate sausages!  Was Traveling from Rome to Paris by sleeper train!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: render :partial => problem

BradHodges wrote:

<%= text_field_tag :name, customer.name %>

No, this is not working: undefined method `name' for nil:NilClass
- I suppose because there is no customer object when I start my application (not before I selected an entry and cause the action)

Even if I catch that issue by

<% if customer != nil %>
         <%= text_field_tag :name, customer.name %>
<% else %>
                Fail!!
<% end %>

He show's me "Fail!!"
If you've got another idea, let me hear. I'm getting insane with that. Enough for now.

BradHodges wrote:

drank a beer and ate sausages!

Nice idea, I will follow that smile Have a nice weekend.

Re: render :partial => problem

:with => "'id=' +$('specificCustomers').value");%>

should be:

:with => "'id=' +$('specificCustomers').value();"%>

If you run Firefox & Firbug, you'd of seen the JavaScript crapping out!

also,  what is specificCustomers ?

What is the HTML element on your page with it's id="specificCustomers"

depending on the type of element, .value() might not be the right call.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: render :partial => problem

BradHodges wrote:

also,  what is specificCustomers ?

What is the HTML element on your page with it's id="specificCustomers"

depending on the type of element, .value() might not be the right call.

"specificCustomers" is a selectbox (obviously with several customer-entries) and your selection is "kept" in that expression. Thats my way of understanding. And actually this is working fine, because when I create an object based on params[:id], I get the right one from the database (by saying @customer = Customer.find(params[:id]) And if I check outputs like

puts "name is " + @customer.name.to_s 

everything seems to be nice.

I will inspect that bug as soon as possible and give a report on that.

Thanks again!