Rails 4.2: Nested form that selects 2 objects from a different model -


i building tricky form. still young rails developer half battle grasping how go this.

i need build form launches campaign. campaign need named , commented. needs choose .zip file form pre-populated dropdown box. under ability choose 1 of 5 .pdf files via check box. zip files , pdf files part of same model upload.

so far campaign model...

models/campaign.rb

class campaign < activerecord::base     has_many :uploads      validates :name, presence: true,                     uniqueness: true      accepts_nested_attributes_for :uploads end 

the uploads model represents table holds zip , pdf files..

models/uploads.rb

class upload < activerecord::base     has_one :campaign end 

here campaign controller..

controllers/campaigns_controller.rb

class campaignscontroller < applicationcontroller       def index         @campaigns = campaign.all.order("created_at desc")       end        def new         @campaign = campaign.new       end        def create         @campaign = campaign.new(campaign_params)          if @campaign.save             flash[:success] = "campaign launched!"             redirect_to @campaign         else             flash[:error] = "there problem launching campaign."             redirect_to new_campaign_path         end       end        def show         @campaign = campaign.find(params[:id])       end    private        def campaign_params         params.require(:campaign).permit(:name, :comment)       end end 

my form looks this...

views/campaigns/_form.html.erb

<%= form_for @campaign, url: {action: "create"} |f| %>      <%= f.label :name %>     <%= f.text_field :name %>      <%= f.label :comment %>     <%= f.text_area :comment %>          #....this lose it..      <%= f.label :data_file %>  <% end %> 

how should got this? case use collection_select? advice or tutorial links highly appreciated.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -