Please tell me how to send variables to partial.

Asked 2 years ago, Updated 2 years ago, 80 views

Prerequisites/What you want to achieve

I would like to send the controller instance variable to the partial.

★Problems/Error Messages you are experiencing

Click on the timeline of the post and click
I created a view so that a modal window like Twitter appears.

view displayed in the modal window クリック Clicked post

If you use one view file, the code will be hard to read, so I put a partial one.

However, the view file with that partial is also partial, so no variables are passed.

As a result, the modal window will no longer display any information about micropost such as the contents...

[microposts/_micropost.html.erb]
Enter a description of the image here
[microposts/_modal.html.erb]
Enter a description of the image here

】Partial flow br
index.html.erb→_user_index.html.erb→_micropost.html.erb→_modal.html.erb

< _micropost.html.erb has been successfully sent...
I think so.

★ Affected Source Code

[page#index]

@user=User.find_by (id:current_user.id)
@microposts=current_user.feed.includes(:comments,:calls,:insists,:says)
@ micropost=current_user.microposts.build

[_user_index.html.erb]

<span class="index-microposts">%=render partial:'microposts/micropost', collection:@microposts,micropost:@micropost%></span>

[_micropost.html.erb]

<%=render'microposts/modal', microposts:@microposts, microposts:@micropost%>

[_modal.html.erb]

<%=simple_format("#{markdown(micropost.content)}")%>

If you don't mind, please tell me how to send the variables.
In the first place, please let me know if you point out that there are too many partials.

multi-post Teratale

ruby-on-rails bootstrap

2022-09-30 21:21

1 Answers

How about changing from @micropost to micropost as follows?

<%=render'microposts/modal', microposts:@microposts, microposts:micropost%>


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.