i have form shows me list of 'challenges' each challenge can have many entries. user able create 'entry' entry has id of parent challenge.
i having difficulty passing id of parent challenge foreign key challenge_id of entry in new form
fields of challenge table are
id :integer rule :string fields of entry table are
id :integer challenge_id :integer title :string the views have are:
challenges/_index
<p id="notice"><%= notice %></p> <table> <thead> <tr> <th>pointsspend</th> <th>rules</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @challenges.each |challenge| %> <tr> <td><%= challenge.pointsspend %></td> <td><%= challenge.rules %></td> <td><%= challenge.id %></td> <td><%= link_to image_tag("list.jpg"), entries_path, {:class => "allentrylink", :"data-challenge_id" => challenge.id} %></td> </tr> <tr> <td><div class="inline allentries" data-challenge_id="<%= challenge.id %>"></div></td> </tr> <% end %> </tbody> </table> <br> entries/_index
<p id="notice"><%= notice %></p> <table> <thead> <tr> <th colspan="3"></th> </tr> </thead> <tbody> <td><%= link_to image_tag("add.jpg"), new_entry_path, id: "newentrylink" %> <% @entries.each |entry| %> <%= entry.blob %> <%= image_tag entry.picture.url if entry.picture? %> <% end %></td> </tbody> </table> <br> <script> </script> the entries/new form
<%= form_for(@entry, html: { multipart: true }) |f| %> <% if @entry.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@entry.errors.count, "error") %> prohibited entry being saved:</h2> <ul> <% @entry.errors.full_messages.each |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :blob %><br> <%= f.text_field :blob %> <%= f.file_field :picture %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> the controllers
class entriescontroller < applicationcontroller before_action :set_entry, only: [:show, :edit, :update, :destroy] def index @challenge = challenge.find(params[:challenge_id]) @entries = entry.where(:challenge_id => @challenge.id) render :layout => false end # /entries/new def new @challenge = challenge.find(params[:id]) @entry = entry.new @entry.challenge_id = @challenge.id end def create @challenge = challenge.find(params[:id]) @entry = entry.new(entry_params) @entry.challenge_id = @challenge.id respond_to |format| if @entry.save format.html { redirect_to @entry, notice: 'entry created.' } format.json { render :show, status: :created, location: @entry } else format.html { render :new } format.json { render json: @entry.errors, status: :unprocessable_entity } end end end def entry_params params.require(:entry).permit(:id, :blob, :challenge_id, :picture) end private # use callbacks share common setup or constraints between actions. def set_entry @entry = entry.find(params[:id]) end # never trust parameters scary internet, allow white list through. def entry_params params.require(:entry).permit(:blob) end end routes
get '/entries/new/:challenge_id', to: 'entries#new' resources :challenges, only: [:new, :create, :show, :index, :edit, :destroy]
Comments
Post a Comment