ruby on rails - How do I pass a parameter to a form partial that is shown via CSS? -


so form partial loaded in div id="secondary", hidden on first page load.

when user hits button class called togglesidebar, _form.html.erb shown.

i have overridden partial display new form (even if update pressed) when user not logged in this:

<%= simple_form_for(post.new, html: {class: 'form-horizontal' }) |f| %>   

as opposed regular version looks this, , included in if statement on same partial:

<% if current_user , current_user.has_any_role? :editor, :admin %>     <%= simple_form_for(@post, html: {class: 'form-horizontal' }) |f| %>  

the real issue in view, when goes update, happens when user logged out:

 <%= link_to "update", "#", class: "togglesidebar" %> 

this perfect, executes css , shows empty form partial perfectly.

however, when user logged in, want send parameter parent_id: @post execution of sidebar being toggled.

this how looks normal new_post_path view (i.e. non-sidebar new post view):

<% if current_user %>                                          <%= link_to "update", new_post_path(parent_id: @post) %> <% end %> 

this postcontroller#new looks like:

  def new     @post = post.new(parent_id: params[:parent_id])   end 

how either pass params in regular non new_post_path version, or tackle way?

you use helper method.

just browse 'helper' directory under 'app' folder , create file similar [name]_helper.rb

in file create module [name]helper , declare helper method in module.

this module automatically required rails.

a small example might you.

the code in link_helper.rb under app/helper directory

module linkhelper      def populate_link(link1, link2, parameter)      if current_user        public_send(link2, parameter)      else        link1      end    end    end

the code in views is

<%= link_to 'update', populate_link('#', 'new_requirement_path',parameter: 33) %>


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -