我是新手,正在开发应用程序。我的用户可以创建一个帖子,该帖子属于该用户。在我的索引用户上,我想为每个用户链接到user_post,我尝试了很多解决方案,但没有任何效果...
_list.html.erb:
<div id="filterrific_results">
<div>
<%= page_entries_info users %>
</div>
<% @users.each do |user| %>
<% if user.coach? %>
<h1><%= user.fname %> <%= user.lname %></h1>
<h2><%= user.mother_tongue %></h2>
<center>
<p><%= user.postal_code %> <%= user.locality %></p>
<p><%= link_to "En savoir plus", **user_post_path** %></p>
</center>
<% end %>
<% end %>
</div>
<%= will_paginate users %>
编辑:路线:
resources :users do
resources :post
end
答案 0 :(得分:0)
用户和帖子之间是否具有has_one关系。 您可以为帖子创建嵌套路线,例如
resource :user do
resource :post, only: [:create]
end
答案 1 :(得分:0)
您说“帖子”属于“用户”,我想这意味着您正在使用 public class CalendarFragment extends Fragment {
RecyclerView recyclerView;
public Calendar currentDate = Calendar.getInstance();
ArrayList<Date> events;
CalendarAdapter calendarAdapter;
Context mContext;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycler_calendar,container, false);
Log.i("onCreateView", "fragment");
recyclerView = (RecyclerView) view.findViewById(R.id.rvNumbers);
mContext = view.getContext();
events = new ArrayList<>();
events.add(new Date());
int numberOfColumns = 7;
final GridLayoutManager gridLayoutManager = new GridLayoutManager(view.getContext(), numberOfColumns);
recyclerView.setLayoutManager(gridLayoutManager);
/*PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);*/
recyclerView.addItemDecoration((new DividerItemDecoration(view.getContext(),DividerItemDecoration.VERTICAL)));
recyclerView.addItemDecoration((new DividerItemDecoration(view.getContext(), DividerItemDecoration.HORIZONTAL)));
updateCalendar(events);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void updateCalendar() {
updateCalendar(null);
}
public void updateCalendar(ArrayList<Date> events) {
Log.i("update", "calendar");
ArrayList<Date> cells = new ArrayList<>();
Calendar calendar = (Calendar)currentDate.clone();
// determine the cell for current month's beginning
calendar.set(Calendar.DAY_OF_MONTH, 1);
int monthBeginningCell = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// move calendar backwards to the beginning of the week
calendar.add(Calendar.DAY_OF_MONTH, -monthBeginningCell);
// fill cells
while (cells.size() < 42)
{
cells.add(calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
// update grid
Log.i("updatecalendar", String.valueOf(cells.size()));
CalendarAdapter calendarAdapter = new CalendarAdapter(mContext, cells);
recyclerView.setAdapter(calendarAdapter);
}
}
and ViewPage adapter class
public class SlideAdapter extends FragmentStatePagerAdapter {
private Context context;
private ArrayList<String> arrayList;
private LayoutInflater mInflater;
LinearLayout layoutSlide;
Drawable d;
ViewPager viewPager;
CalendarFragment calendarFragment;
List calendarFragmentList = new ArrayList();
public SlideAdapter(FragmentManager fragmentManager, List calendarFragmentList) {
super(fragmentManager);
this.context = context;
this.calendarFragmentList = calendarFragmentList;
Log.i("SlideAdapter", "constructor");
}
@Override
public int getCount() {
return calendarFragmentList.size();
}
@Override
public Fragment getItem(int position) {
return (Fragment)calendarFragmentList.get(position);
}
}
和has_many
。如果不是这种情况,请注意复数与单数有关。鉴于此,您需要从belongs_to
中提取特定帖子。请参阅以下内容:
ActiveRecordRelation
这假设您已经设置了路线<% @users.each do |user| %>
<%= user.fname %>
#etc
<% user.posts.each do |post| %>
<%= link_to post.title, post %>
<% end %>
<% end %>