I have a simple rails view with 2 different leading to 2 different controller routes.
'login' <form> leading to a '/login' route.
'create' <form> leading to a '/create' route.
I can only seem to pass my parameters to my controller from whichever form is higher up on my DOM.
How can I get the params from the second form to pass to my controller as well?
Related
I what to use the same razor page in more than one position in my web app.
I have the page /one and I want to reach also with /two and /three.
I can do it with the AddPageRoute :
options.Conventions.AddPageRoute("/one", "two");
options.Conventions.AddPageRoute("/one", "three");
But the taghelpers in the page always render formaction="/three?handler=Update" pointing to the last route, same for /one, /two or /three
This make impossible use the same razor page in different position in my web application.
Git sample : https://github.com/enricoe73/OnePage2Routes.git
You can reach the page using any of the routes you defined, but the outbound URL generation components in the framework (tag helpers, IUrlHelper etc) will always use the last route registered when generating an outbound URL, so the order in which the routes are registered plays a part.
If that doesn't suit you (because you may want to point to different routes at different times, for instance), the solution is to use the standard HTML formaction attribute instead of the custom asp-* attributes that belong to the tag helper:
<button formaction="/two?handler=update">Update</button>
Hi I am trying to push a page but it appears any push to a page I'm currently on doesn't reload the page like /about to /about doesn't do anything(which is fine). If I push a page that is different than the one I'm on it will direct me to the page and reload(expected). The problem is I am using a param in the url that affects what is shown like /profile/jordan1 and when I push /profile/jordan2 because its the same base route and only parameter change it does show /profile/jordan2 in the URL but does not actually reload the page so the page doesn't repopulate with jordan2 data and stays unchanged so I can reload the browser myself and it will work but I want the button click to do that like it should do
Im using Vuetify and have tried just using :to in the button component as well as #click and creating a method with
:to="'/profile/' + this.$store.state.userAuthData.username"
this.$router.push({ name: 'Profile', params: { username: this.$store.state.userAuthData.username } })
both work Identical and have same issue.(the :to one I'm actually using a computed property with that route but for simplicity just wrote it like this)
I've started using Piranha CMS and really enjoining it, but now I came into a bit of trouble.
One of my pages is a contact form where I want to post an extended page model with contact information to the controller.
I've created a page template Contact and in the manager gui and I've set the setting View to Contact
The correct view is loaded but the problem is that all the requests goes against the index action method and not the contact action method in PageController.
What am I doing wrong?
The view is used to signal the controller what view should be used to render the page, not which action should be called. This can be used when several pages has the same type of data and logic, but should be rendered differently.
The field route is used to rewrite a page to a controller/action. The default route for a page is Page which means that requests to that page is rewritten to ~/page, i.e the PageController and its Index action. If you wanted a certain type to be rewritten to the contact action of the PageController you set the route to Page/Contact which will rewrite the request to ~/page/contact.
If you have a complex structure you should add custom controllers, for examples a ProductController. The route could then be set to Product rewriting the request to ~/product.
I hope this clarifies things!
Regards
HÃ¥kan
If I am on a page and decide to click cancel, I want to redirect it to an action method with parameters. But to do that, what is the way to hold the action parameter values on the page and use them later on a click of link or button.
One way is to create hidden fields in the page while page is rendered and hold the values in them. Extract the value from them using Java script and alter the documen
Is this the right way to do it in mvc or there is a cleaner way to accomplish this. Please suggest.
In case you need to use a button you will need to use jquery (or vanilla java script) to redirect user somewhere by button. As an option you can create button with, for example, data-url attribute and use #Url.Action helper method (instead of Html.ActionLink. ActionLink generates entire a tag with all html parameters like href and other which you provide, but Url.Action generates only url - the value of href attribute) to generate link with parameters and implement button click event handler to redirect user by this url. Example:
Button (view, Razor syntax):
<input type="button" data-url="#Url.Action("Action","Controller", new { parameter=value }, null)" value="Click me" id="my-button"/>
Related jquery:
$(document).ready(function(){
$('#my-button').click(function(){
window.location.href = $(this).data('url');
});
});
1) if you already have the parameters in database.
All you need to is pass a id or something of the clicked data from which you can retrieve the required values in the controller.
In your syntax pass the values.
#Html.ActionLink("cancle","ActionName","ControllerName", new { id = Model.Id, type = "button" })
use this id in the controller and get the parameters from database and proceed.
2)if parameters are obtained dynamically (not available in DB).
your approach will be good (by saving it in hidden fields and later retrieving in controller or processing it in JS).
Is there a way to render a partial view with accompanying code in rails?
For instance: I want to be able to create a partial view which will show the top 5 foobars on my site. This partial needs accompanying code to retrieve some foobars from the database, rank them according to an algorithm, and then output the view with the top 5.
I want to be able to include this partial on any page I fancy, preferably just by using something like
<%= render :action => "top_five_foobars" %>
Is this doable? I'm used to asp.net mvc where you can create an action that runs some code and returns a partial, but it seems like in rails it returns simply the template...
If google get you here, you might be looking for cells
Cells are view components for Rails. They are mini-controllers with
their own MVC stack, can invoke logic and render views. They bring
back OOP to Rails' view layer and make writing reusable portlets for
your applications fun.
You need something like a shopping cart, which appears on almost
every page of your app.
You wouldn't use a partial and a helper, would you?
It might not be the cleanest way, but what I did is that I created a helper method in the Application Controller that retrieves the top 5 foobars. Then I call this method in the views. I also cached the part of the view that shows the results.