ruby on rails - Passing attributes while using a partial inside another view -
i have 3 models - company, user(using devise) & donation.donation rich join between company , user using :through. have partial form creating new donation inside show view of company takes 3 attributes - company_id, user_id , amount. code form inside view follows:
<%= simple_form_for @donation |f| %> <div class="row"> <div class="container"> <div class="col-sm-6"> <div class="well"> <h1 align="center" class="amounthead">donate</h1> <form class="form-inline donate-form"> <div class="form-group"> <div class="input-group"> <div class="input-group-addon">₹</div> <%= f.input :amount, label: false, placeholder: 'amount in rupees' %> <div class="input-group-addon">.00</div> </div> </div> <div class="actions" style="text-align: center"> <%= f.submit :class => 'btn btn-success' %> </div> </form> </div> </div>
how pass user_id , company_id using id of current user , id of company viewing automatically when user enters amount.
donation_controller.rb
class donationscontroller < applicationcontroller def index @donations = donation.all end def new @donation = donation.new() end def create @donation = donation.new(donation_params) respond_to |format| if @donation.save format.html { redirect_to @donation, notice: 'donation created.' } format.json { render :show, status: :created, location: @donation } else format.html { render :new } format.json { render json: @donation.errors, status: :unprocessable_entity } end end end def donation_params params.require(:donation).permit(:amount,:user_id,:company_id) end end
company_controller.rb
def show @donation = donation.new end
you can use hidden_field
in form
user_id
, company_id
f.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } f.input :company_id, :as => :hidden, :input_html => { :value => @company.id }
you need define @company = company.find(params[:id])
in companies_controller
show method this.
Comments
Post a Comment