Note that there are some explanatory texts on larger screens.

plurals
  1. POView with data from different tables connected with foreign key
    primarykey
    data
    text
    <p>For my project, I try to understand how to let data view on a single view from different tables. I tried to implement the example of the Ruby on Rails Tutorial (<a href="http://ruby.railstutorial.org/chapters/user-microposts#sec:a_micropost_model" rel="nofollow noreferrer">http://ruby.railstutorial.org/chapters/user-microposts#sec:a_micropost_model</a>). In this example I'm able to see the microposts of a user on the user's page. So what was I doing? </p> <ol> <li>I generated a table for user with <code>rails generate scaffold user name:string</code> + rake db:migrate</li> <li>I generated a table for microposts with <code>rails generate scaffold micropost content:string user_id:integer</code></li> <li>I added into <strong>db/migrate/..._create_microposts.rb</strong> <code>def self.up (...) add_index :microposts, :user_id add_index :microposts, :created_at end</code></li> <li>I added the foreign/primary-key- relations into <strong>microposts- model</strong> <code>belongs_to :user</code> <strong>&amp; users- model</strong> <code>has_many :microposts</code></li> <li>rake db:migrate </li> <li>I changed the users-Show-html-View: <strong>app/views/users/show.html.erb</strong> <code>&lt;% unless @user.microposts.empty? %&gt; &lt;%= render @microposts %&gt; &lt; %end% &gt;</code></li> <li>For the command "render", I paginated the micropostpage <strong>app/views/microposts/_micropost.html.erb</strong> with content <code>&lt;% = micropost.content %&gt; posted &lt;% = time_ago_in_words(micropost.created_at)%&gt; ago</code></li> </ol> <p>I've already filled some content and rails console says that rails already knows the link between both tables. Anyways, if I open the show action, I get some error: </p> <hr> <pre><code>NoMethodError in Users#show Showing c:/src/script/testtest2/app/views/users/show.html.erb where line #13 raised: undefined method `model_name' for NilClass:Class Extracted source (around line #13): 10: &lt;/p&gt; 11: 12: &lt;% unless @user.microposts.empty? %&gt; 13: &lt;%= render @microposts %&gt; 14: &lt;% end %&gt; 15: 16: &lt;%= link_to 'Edit', edit_user_path(@user) %&gt; | Rails.root: c:/src/script/testtest2 Application Trace | Framework Trace | Full Trace app/views/users/show.html.erb:13:in `_app_views_users_show_html_erb___161211003_22878180__702430867' app/controllers/users_controller.rb:18:in `show' Request Parameters: {"id"=&gt;"1"} </code></pre> <p>It seems like rails doesn't like "render", so what did I do wrong? Unluckily I skipped some chapters of the tutorial, because I just simply want to know how to list microposts on the User-show page. Do I need to define more? In the tutorial, there's much more code, but this code also contains lots of definition for the css-Stylesheet, so I'm confused which code is really necessary for simply solving my problem. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload