Topic: scaffold has_many

How do you make a scaffold and refer to a has_many?

I have a user class and now i want to create skills and be able to say that this user has many skills



rails generate scaffold Skills name:string user:int

Re: scaffold has_many

Read this:

http://guides.rubyonrails.org/getting_started.html

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: scaffold has_many

Thanks i was fallowing that same tutorial.
and i was wondering if there was a better way of adding the comment to the post

Re: scaffold has_many

The way it shows you to do it on that tutorial is the best way to go about it for that example. What else are you trying to do?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: scaffold has_many

Well i was wondering if i could do it with a scaffold with out of having to manually create the models, views, and controllers.

Re: scaffold has_many

but i did like how they did it i did understand why he does it that way.
Back to that tutorial

When i add a delete comment there is a extra delete at the end that i cant find out how to get rid off

Re: scaffold has_many

Two delete links or popups?

Post the code so we can see. (use [ code ] tags to make it easy to read)

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: scaffold has_many

on app/views/skills/_skill.html.erb

<p>
  <%= link_to 'Delete Skill', [skill.user, skill],
    :confirm => 'Are you sure?',
    :method => :delete
    %>
  <%= skill.skill_name %>
</p>

On app/views/users/show.html.erb

<h2>Skills:</h2>
<%= render @user.skills %>

Re: scaffold has_many

And it's showing two delete links?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: scaffold has_many

yes it is

Re: scaffold has_many

From the code you showed me, only one link should be showing. So there has to be something wrong elsewhere in your code

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: scaffold has_many

do you have any idea where to start looking?

Re: scaffold has_many

Your view file. Paste it here

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: scaffold has_many

app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
<title><%= @page_title %> </title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
  <link rel="stylesheet" href="/css/screen.css" type="text/css" media="screen, projection">
</head>
<body>
  <div class="container">
    <h1>TEST</h1>
    <hr>
    <%= yield %>
        <div class="copy">
          &copy;  2012
        </div>
    </div>
  </div>
</body>
</html>

app/views/users/show.html.erb

<%# SKILLS %>
<h2>Add a Skill:</h2>
<%= render "skills/form" %>
<h2>Skills:</h2>
<%= render @user.skills %>

app/views/skills/_skill.html.erb

<p>
  <%= link_to 'Delete Skill', [skill.user, skill],
    :confirm => 'Are you sure?',
    :method => :delete
    %> &nbsp;
    &nbsp;
    &nbsp;


  <%= skill.skill_name %>
</p>

Re: scaffold has_many

Do both delete links link to the same URL?

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: scaffold has_many

the one that i want to remove does not have a id at the end of the path

<p>
  <a href="/users/1/skills/17" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete Skill</a>  new skill
</p>
<p>
  <a href="/users/1/skills" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete Skill</a>
</p>

Last edited by moiseszaragoza (2012-04-10 08:53:15)

Re: scaffold has_many

You might have an erroneous entry in your database that is causing a second entry to show (including it's Delete link).

Open your rails console and write:

Skill.destroy_all

This will remove everything in your skills database. Then you can re-add what you had, but maybe the extra won't show. Worth a shot

Remember to edit your topic title and add "[SOLVED]" if your question has been answered!

Follow me!

Re: scaffold has_many

What happens if you change your app/views/skills/_skill.html.erb to this (just deleted second skill in array):

<p>
  <%= link_to 'Delete Skill', skill.user,
    :confirm => 'Are you sure?',
    :method => :delete
    %> &nbsp;
    &nbsp;
    &nbsp;


  <%= skill.skill_name %>
</p>

or this:

<p>
  <%= link_to 'Delete Skill', skill,
    :confirm => 'Are you sure?',
    :method => :delete
    %> &nbsp;
    &nbsp;
    &nbsp;


  <%= skill.skill_name %>
</p>

Last edited by Brian71 (2012-04-10 09:42:24)

Re: scaffold has_many

The First one deleted the user but it does not solve the loop problem

the second one it gives a error that the skill method is undefined

and i want to say thanks for the help i do appreciate it

Re: scaffold has_many

What's the loop problem?

You may want to try using 'debugger' - RailsCasts has an updated video that's pretty good - shows you how to step through your code to see what's happening so that you can troubleshoot more easily.

Here's the link:
http://railscasts.com/episodes/54-debug … by-revised