Rails3 nested layout and partials passing parameters - ruby-on-rails-3

Hey,
I'm working on a page having a nested layout. first I have the applicationlayout with my "mainmenu" now I want to add a second menu only on this page. I got this working via
<% render :partial => "mypartial", :layout => 'navigation' %>
this adds my second navigation to the form and renders a partial.
At this point I try to distinguish between two different partials. so my file looks like this
<% if :passed_text == "page1" %>
<%= render :partial => "mypartial1", :layout => 'navigation' %>
<% else %>
<%= render :partial => "mypartial2", :layout => 'navigation' %>
<% end %>
my navigation is as follows:
<%= link_to "Mypartial1", partial_path, :passed_text => :page1 %>
<%= link_to "Mypartial2", partial_path, :passed_text => :page2 %>
<%= yield %>
but it ignores my parameters. I guess I'm missing something basic, but all this is new to me.
thanks for your help

okay I found an answer:
first I have to check for:
params[:passed_text]
instead of :passed_text
secondly passing the parameters has to be in brackets
partial_path( :passed_text => :page1)
this works fine

Related

undefined method `model_name' in a partial rendered by different controller

I'm trying to render this form:
<form class="form-inline">
<%= simple_form_for #prospect,
:url => url_for(:action => 'create', :controller => 'prospects'),
:method => 'post' do |f| %>
<%= f.error_notification %>
<%= f.input :name, placeholder: 'Name', label: false %>
<%= f.input :email, placeholder: 'Email', label: false %>
<%= f.input :interests, placeholder: 'Tell us what you were searching for', label: false, value: params[:search] %>
<%= f.error :base %>
<%= f.button :submit, "Submit", :class=> "btn" %>
<% end %>
Using this partial:
<%= render partial: 'prospects/novideo_capture' %>
The partial is in a view controlled by Videos#index controller, and I keep getting this error: 'undefined method `model_name' for NilClass:Class'
This is my prospects controller:
class ProspectsController < ApplicationController
def index
#prospects = Prospect.all
end
def new
#prospect = Prospect.new
end
def create
#prospect = Prospect.new(params[:prospect])
if #prospect.save
render "thanks_for_interest"
else
render "novideo_capture"
end
end
I'm not sure what I'm going wrong, although I'm pretty sure it's a simple solution. I've seen a lot of similar questions around SO and tried all their answers, but none of them seem to work for this situation.
Thanks for any help...
EDIT: Adding
#prospect = Prospect.new
to the videos index controller stops the error occurring, but I don't feel it's the right way to do this. It also doesn't actually make the form use the prospects controller.
EDIT2: I now have the partial rendering correctly (I think), and my videos#index calls the partial like this:
<%= render partial: 'prospects/novideo_capture', :prospect => #prospect %>
Then simple_form in the partial looks like this:
<form class="form-inline">
<%= simple_form_for :prospect,
:url => url_for(:action => 'create', :controller => 'prospects'),
:method => 'post' do |f| %>
...
<% end %>
However it's not actually submitting the form with the prospects controller. Any ideas why?
Check your markup. You're wrapping a simple_form inside another form. Since the first form tag has no action associated with it (<form class="form-inline">), that form will submit against the current URL, which is the video#index.
You're going to want something like this:
<%= simple_form_for :prospect, :url => etc, :method => 'post', :class => "form-inline" do |f|
...
<% end %>
Losing the leading (redundant) form-inline form tag and you'll be fine.

Is it possible to render partial using link_to helper?

I have a Rails 3 application with simple navigation that contains links to controllers actions using link_to helper.
Now, I need to add a link which will refer to partial. Is it possible to combine the link_to and render: partial methods?
Generally, what I need is to combine somehow the following codes and have a link_to which is rendering the partial:
<%= link_to 'List', security_users_path , remote:true, class:'e-icon e-icon-camera' %>
<% #security_user = SecurityUser.new %>
<%= render partial: 'security_users/sign_in', :locals => {:security_user => #security_user} %>
If it is not possible, what other alternatives I have if I want to render a partial?

Partial not accessing local variable

I am rendering a partial like so:
<% #pages.each do |page| %>
<%= render 'layouts/pagewithchildren', :locals => { :page => page } %>
<% end %>
But when i try to access a variable in page i am getting the error:
undefined local variable or method `page'
I am accessing the variable like:
<%= page.title %>
So what else do I need to do?
i'm not 100% sure but isn't it either
<%= render 'layouts/pagewithchildren', :page => page %>
or
<%= render :partial => 'layouts/pagewithchildren', :locals => { :page => page } %>
?
You have to explicitly specify partial, otherwise, Rails will treat locals as a params hash, you can access locals[:page] but not page variable directly in your partial.
Change your code to:
<%= render partial:'layouts/pagewithchildren', locals: {page: page} %>

Why is my nested text_area helper adding html tags?

I have a text_area in a partial in a complex form that is called like so
<%= f.fields_for :notes do |notes_form| %>
<%= render :partial => 'note', :locals => {:f => notes_form, :operation => f, :count => operation.notes.count} %>
<% end %>
<p><%= add_child_link "Add note", :operation_notes %></p>
and the partial looks like this
<% count ||= 2 %>
<div class='fields'>
<%= f.text_area :note_text, :rows => "4", :class => "notes" %>
<%= remove_child_link "x", f, count %>
</div>
There can be many notes on the form hence the add and remove child links.
The issue I'm having is that if I add a note with the text 'abcd', when I bring up the edit form I get '<p>abcd</p>'. If there are line breaks in the note it adds <br /> tags. The text_area form helper seems to be using the simple_format helper but I have no idea why. Can anyone help as this is very undesirable behaviour?
Ah solved,
Earlier on the same page I was displaying the note and using simple_format to format it with
<%= simple_format note.note_text %>
It seems that simple_format is somewhat destructive as after this, a call to note.note_text always returns the formatted text. If I change the above to
<%= simple_format note.note_text.dup %>
then the note_text attribute is not altered and I get the appropriate results.
I will have to look more closely at simple_format but this really strikes me as undesirable behaviour.
EDIT
It looks like this has been corrected in Rails 3.1
I would suspect that you have something in your Note model that is processing the text. Check for callbacks in this model.

Rails 3 Showing image_tag with different style with if statement

I am trying to change the style of an image created with image_tag. Below is my current code.
<% if #user.image? %>
<%= link_to image_tag(#user.image_url(:medium), :style => "width:717px;height:409px"),
#user.image_url(:medium1024), :rel => "facebox" %>
<% else %>
<%= image_tag 'i.jpg', :style => "width:717px;height:409px" %>
<% end %>
I want to be able to change the :style => "width:717px;height:409px" to :style => "" dependent on whether #user.machine is imac, macbookpro, pc or laptop.
Assuming that you have set up #user.machine in your controller I would use the image_tag to create a class based on the value of machine. If I were you I would simply create css classes that correspond to #user.machine's values so you can link them up like this:
<%= link_to image_tag(#user.image_url(:medium), :class => "#{#user.machine}") %>
This will spit out HTML like this (depending on what #user.machine is set to:
<img class="macbookpro" src="imageurl" />