开发者

Problems for link on validation failure

开发者 https://www.devze.com 2023-03-23 21:57 出处:网络
I have an attachment models that you can add to an asset when you click a link.On this page to upload the attachment I have this for a back link:

I have an attachment models that you can add to an asset when you click a link. On this page to upload the attachment I have this for a back link:

<%= link_to 'Back', asset_path(@asset.id)%>

however, when it fails a vaidation it gives a big error and will not display. How can I fix this?

UPDATE:

For those wanting to know the error is this:

Called id for nil, which would mistakenly be 4 -- if you really wanted开发者_JAVA技巧 the id of nil, use object_id

Extracted source (around line #5):

3: <%= render 'form' %>
4: 
5: <%= link_to 'Back', asset_path(@asset.id)%>

NOTE: This works fine when it passes validations but not when it does not, asset is being built in the new method of the attachment controller by:

@asset = Asset.find(params[:asset_id])
@attachment = @asset.attachments.build


Your link to should be inside an unless:

<% unless @asset.new_record? %>
  <%= link_to 'Back', asset_path(@asset.id)%>
<% end %>

If the @asset object was not saved already, the asset_path will fail as id will be nil.


Maybe in your create action you are not setting an @asset variable?

If you are calling render :new, it doesn't mean that all variables from new action are magically initialized.

I think this can help: The Difference Between render and redirect_to

0

精彩评论

暂无评论...
验证码 换一张
取 消