How to pass the parameter to the partial part of the form that is shown through CSS?

So, my form is partially loaded in my div id="secondary" , which is hidden when loading the first page.

When a user clicks a button with a class called toggleSidebar , toggleSidebar is displayed.

I redefined partial to display the new form (even if I click update ) when the user did not log in as follows:

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

Unlike the regular version, which looks like this and is included in the if in the same partial:

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

The real problem in my opinion when someone goes to update is what happens when the user logs out:

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

This is great, it does CSS, and it shows the blank form perfectly.

However, when the user is logged in, I want him to send the parent_id: @post when starting the sidebar.

This looks with the usual new_post_path (i.e. the new postbar view):

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

This is what my PostController#New looks like:

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

How can I pass parameters in the regular version not new_post_path or solve this other way?

+5
source share
3 answers

Perhaps you can use a helper method.

Just go to the "helper" directory in the "app" folder and create a file similar to [name] _helper.rb

In this file, create a module using the [name] Helper and declare your helper method in this module.

This module is automatically required by the rails.

A small example may help you.

Code in link_helper.rb in the app / helper directory

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

Code in Views

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

I'm a bit confused by the question, but I think you just need to use a hidden field to return param_id param?

eg /

 <%= simple_form_for(Post.new, html: {class: 'form-horizontal' }) do |f| %> <%= f.hidden_field :parent_id, { value: @post.try(:id) } %> <% end %> 

NTN?

+2
source

I'm also a little confused, but the next railscast may help you. It shows how to embed data in an html tag. You can probably do it the same way. railscast-> data transfer in javascript

Of the possibilities there, I would recommend the data attribute:

 <%= simple_form_for,(Post.new, html: {class: 'form-horizontal' }, **data: {post_id: @post.id}**) do |f| %> <% end %> 
+1
source

Source: https://habr.com/ru/post/1207799/


All Articles