Generate a 'Link_To' to the Controller Action 'Edit', Dynamically

Generate a `link_to` to the controller action `edit`, dynamically

You can write routes using the "array style" like this :

= link_to "Edit", [:edit, @your_resource]

Rails dynamic link_to's

You have

<%= link_to job.id, job %>

Here second option specifies your url or path helper. You are getting this error because there is no job_path helper for your routes

To check all the url or path helpers do rake routes in your terminal. It will give you output which looks something like

Prefix       Verb   URI Pattern                                     Controller#Action

customer_job GET /customers/:customer_id/jobs/:id(.:format) jobs#show

So replace your link with

<%= link_to job.id, customer_job_path(@customer,job) %> 

UPDATE:

Referring to your comment:

@customer is an instance variable of your Customer model which you would have defined in your controller action(since you are using it in your view). Instance variables defined in your controller action are automatically available for its view. For details you should read docs

create a dynamic link text and link in Html.ActionLink()

It should be like:
Html.ActionLink("Details", "Country", new { country = @item })

Or try like this:

<a href="@Url.Action("Details", "Country", new { country = @item })">@item</a>

you are getting null, as you are not passing the parameter to your action method.

Dynamically generate link_to for different models using common partial - Rails 4

You can use layouts with your partials. See the API docs for more details.

Rendering pictures with layouts

First, rewrite pictures/index.html.erb to:

<%= render partial: 'picture', layout: 'shared/index_common_grid', collection: @pictures, as: :object %>

Second, rewrite shared/index_common_grid.html.erb to:

<div class="col-sm-4 pull-left">
<div class="thumbnail">
<div class="caption">
<h4><%= object.title.capitalize%></h4>
<p class="text-muted">
<%= yield %> | <span class="pull-right"><%= show_formatted_date(object.created_at)%></span>
</p>
</div>
</div>
</div>

Thrid, create pictures/_picture.html.erb:

<%= link_to "Edit", [:edit, current_user, @request, @shop, picture]%>
| <%= link_to "Delete", [current_user, @request, @shop, picture], :data=>{:confirm=>"Are you sure ?"}%>

Adding other types of objects

If you want to render other types of objects (menus in your example) then:

  1. Add menus/index.html.erb with a content similar to pictures/index.html.erb (replace picture and @pictures with menu and menus).
  2. Add menus/_menu.html.erb with links rendered appropriately for menus.

Create dynamic links in the navigation panel

if the controller and action don't match an existing controller and action then no href will be rendered. I suspect that @category.DisplayName does not match any actual action name on your home controller. Seems more likely that you have an action named Category that expects a parameter corresponding to the @category.DisplayName so it should be passed as a route parameter not as the action name

@foreach (var category in categories)
{
<li><a asp-controller="Home"
asp-action="Category"
asp-route-category="@category.DisplayName">@category.DisplayName</a></li>
}


Related Topics



Leave a reply



Submit