forms - Rails links not connecting properly -


i rails beginner , have created 3 models/controllers/views using rails generate scaffold:

  • subjects, have many topics
  • topics, have many notes
  • notes

when go http://localhost:3000/subjects/1/topics, rails lists empty list of topics , when 'new topic' link clicked, taken http://localhost:3000/topics/new.

should , how link 'new topic' take user http://localhost:3000/subjects/:id/topics/new instead of http://localhost:3000/topics/new , should new topic form submit http://localhost:3000/subjects/:id/topics/new instead of http://localhost:3000/topics?

views/topics/index:

<h1>listing topics</h1>  <table>   <tr>     <th>name</th>     <th></th>     <th></th>     <th></th>   </tr>  <% @topics.each |topic| %>   <tr>     <td><%= topic.name %></td>     <td><%= link_to 'show', topic %></td>     <td><%= link_to 'edit', edit_topic_path(topic) %></td>     <td><%= link_to 'destroy', topic, method: :delete, data: { confirm: 'are sure?' } %></td>   </tr> <% end %> </table>  <br />  <%= link_to 'new topic', new_topic_path %> 

controllers/topics:

def new   @topic = topic.new    respond_to |format|     format.html # new.html.erb     format.json { render json: @topic }   end end  def edit   @topic = topic.find(params[:id]) end  def create   @topic = topic.new(params[:topic])   @topic.subject_id = params[:project_id]   respond_to |format|     if @topic.save       format.html { redirect_to subject_path(@topic.subject_id), notice: 'topic created.' }       format.json { render json: @topic, status: :created, location: @topic }     else       format.html { render action: "new" }       format.json { render json: @topic.errors, status: :unprocessable_entity }     end   end end 

new topics form:

<%= form_for(@topic) |f| %>   <% if @topic.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@topic.errors.count, "error") %> prohibited topic being saved:</h2>        <ul>       <% @topic.errors.full_messages.each |msg| %>         <li><%= msg %></li>       <% end %>       </ul>     </div>   <% end %>    <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> 

routes:

resources :subjects   resources :topics     resources :notes   end end  resources :notes   resources :topics   resources :subjects  root :to => 'subjects#index' 

when go http://localhost:3000/subjects/1/topics action index of controller topicscontroller called params[:subject_id] set 1.

so in controller action, have check parameter , filter topics if given

def index   if params[:subject_id].present?     @subject=subject.find params[:subject_id]     @topics=@subject.topics   else     @topics=topic.all   end end 

and in index view, have use url, if @subject ist present:

<%= link_to 'new topic', @subject.present? ? new_subject_topic_path(@subject) : new_topic_path %> 

in topics new action again :subject_id parameter:

def new   @subject=subject.find params[:subject_id]   @topic = @subject.topics.new end 

then in topics form, can forward subject_id in hidden field:

<%= form_for(@topic) |f| %>   ...   <%= f.hidden_field :subject_id %>   ... <% end %> 

the rest of tobicscontroller can stay same. topic assiciated subject subject_id


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -