Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing an object through a link_to command to be referenced in the model#new page
    primarykey
    data
    text
    <p>I'm creating a basic loyalty card application, with model Merchant and Loyaltycard. In the merchant#show view I have this line</p> <pre><code>&lt;%=link_to 'New Loyalty card', new_loyaltycard_path(:merchant_id=&gt;1) %&gt; </code></pre> <p>I'm trying to pass the merchant ID to the loyaltycard#new view so it will be automatically selected as the merchant for that card. In loyaltycard#_form (which gets called by loyaltycard#new) I have the lines</p> <pre><code> &lt;%if @merchant%&gt; &lt;%= f.hidden_field :merchant_id, :value =&gt; @merchant.id %&gt; &lt;%else%&gt; &lt;div class="field"&gt; &lt;%= f.label :merchant_id %&gt;&lt;br /&gt; &lt;%= f.text_field :merchant_id %&gt; &lt;/div&gt; &lt;%end%&gt; </code></pre> <p>But I keep getting and error that says can't call id for class Nil. Is there a better way of doing this?</p> <p>Here is the controller code for loyaltycard</p> <pre><code> def new @loyaltycard = Loyaltycard.new respond_to do |format| format.html # new.html.erb format.json { render json: @loyaltycard } end end # GET /loyaltycards/1/edit def edit @loyaltycard = Loyaltycard.find(params[:id]) end # POST /loyaltycards # POST /loyaltycards.json def create @loyaltycard = Loyaltycard.new(params[:loyaltycard]) respond_to do |format| if @loyaltycard.save format.html { redirect_to @loyaltycard, notice: 'Loyaltycard was successfully created.' } format.json { render json: @loyaltycard, status: :created, location: @loyaltycard } else format.html { render action: "new" } format.json { render json: @loyaltycard.errors, status: :unprocessable_entity } end end end </code></pre> <p>The error is </p> <pre><code>Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload