Conditional render partials

Hi,all

I’ve been trying to get this to work without any luck.

in my _edit.rhtml view, I have the following thing

<%= form_remote_tag( :url=> {:action => 'check_place'} ,:before => "$('place2').value = $('article_site').value") %> <%= text_field_tag('place2') %> <%= submit_tag('Check Place2') %> <%= end_form_tag %>

in the controller,I have the function
def check_place
place = Place.find(:first, :conditions => [%{location = ?
},params[:place2].downcase])
if place.nil?
render :partial => ‘get_more_info’
end
end

the _get_more_info.rhtml is something like this

<%= javascript_include_tag :defaults %>

<%= error_messages_for ‘article’ %>

Please enter some details

so I expect this partial to be displayed when place.nil? is true.
however, even if the program can get into this view, the view does not
display at all.

anybody having some ideas? I suspect the view is not written
properly,something is missing.

Thanks a lot!

Hi, thanks for the post. Anyway, could you explain exactly what
you’re trying to do?

-Conrad

sure,so I need to know whether the place I entered exists in my database
or not. if it is in the database then I print out Query OK. If it does
not exist,I want to invoke a form asking for more information.

so in _edit.rhtml,I press Check Place button to trigger the check_place
function in the controller. and in the controller I try to figure out if
that place exists. and if it doesnt(place.nil?=true),I need to
render_partial “get_more_info”.

So it all worked fine until it gets to the get_more_info partial.
whatever I write in that partial file _get_more_info.rhtml, it doesnt
get displayed at all. however, as I can see, the controller is able to
find _get_more_info.rhtml.

I suspect something might be wrong in _get_more_info.rhtml but not sure
where exactly the problem is.

Any hints?

Thanks for you help!

Hi,

I have a table storing greeting messages that I need to be converted to
html.

Is there a rails way of doing this?

Thanks in advance,
Chris.

Thanks for the reply,Jeff.

Yes,I know development.log, thanks for reminding me. the thing is
whether to render this partial get_more_info is controlled by the
controller. so how would I do that in the edit.rhtml view?

Jeff P. wrote:

Jacquie F. wrote:

Hi,all

I’ve been trying to get this to work without any luck.

in my _edit.rhtml view, I have the following thing

<%= form_remote_tag( :url=> {:action => 'check_place'} ,:before => "$('place2').value = $('article_site').value") %> <%= text_field_tag('place2') %> <%= submit_tag('Check Place2') %> <%= end_form_tag %>

SNIP... so I expect this partial to be displayed when place.nil? is true. however, even if the program can get into this view, the view does not display at all.

anybody having some ideas? I suspect the view is not written
properly,something is missing.

Thanks a lot!

Hi Jacquie,
Do you know about “development.log”? This is a log file that often has
some useful info in it when things are going poorly. You will find it
in the “logs” folder in your Rails project root.

Just taking a stab at this from what you’ve said, I’m a little unsure
about how you’re trying to do the render :partial from the controller.
There’s more than one way to skin a Republican, but what I would be
doing in this instance is to have something like:

<%= render :partial => 'get_more_info' %>

at an appropriate spot in my “edit” view. Then in my conditional stuff
in the controller I would use something like:
render :update do |page|
page.visual_effect :BlindDown, “error_stuff”, :duration => 1.0,
:queue => “end”
end
to make the error stuff slide into view with ajax.

Another thing to consider that would be simpler would be to use the
“Flash” helper in Rails as described on page 88 of the Agile Rails book:
<% if @flash[:notice] -%>

<%= @flash[:notice] %>
<% end -%>

Then your controller just has to:
flash[:notice] = ‘Please Enter Some Details’

hth,
jp

  1. use <%= debug(‘place’) %> to check your variable in the view

  2. I would move the conditional statements to the view like this:

<% unless place %>
<%= render :partial => ‘get_more_info’ %>

Can you clarify the problem you’re having?

Jason

You’re doing <%= error_messages_for ‘article’ %> in your partial, but in
check_place you’re not defining @article (unless there is some other
code you haven’t shown).

A more general tip. Download Firebug now. I mean it. It’s a firefox
extension that I consider absolutely vital when working with ajax, you
can see all the ajax requests you have made (and their response), it
flags javascript errors, has a dom inspector etc…

Fred

Jacquie F. wrote:

Thanks for the reply,Jeff.

Yes,I know development.log, thanks for reminding me. the thing is
whether to render this partial get_more_info is controlled by the
controller. so how would I do that in the edit.rhtml view?

Jeff P. wrote:

Oh! I think I understand how you are going at this thing. Have a look
at “replace_html” instead of “render :partial”.

The problem is you are trying to render a partial from your controller,
and AFAIK that can’t be done. There is nothing to tell it where in the
page to render it.

From your original post, it looks like you’re doing this in ajax land,
so in your controller you can do a render :update with replace_html.
You need to put an empty “div” in your edit view where you want it to
show up and give the div an “id”. Then replace that div with your
partial using “replace_html” and render :update.

Make sense?

hth,
jp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs