Asp.Net MVC - RenderPartial - Create in a List view - asp.net-mvc

I got a page that lists all my articles (Articles/List.aspx).
I also got a control that create article (Article/Create.ascx).
I will like that my List.aspx page that's render the Create.ascx to be able to create article.
I know that in MVC, the preferred approach is one page by action. But in this case I need to do that. It's a design issue and how the client want the Web site to work.
So for now, I got the following code in List.aspx :
<% Html.RenderPartial("Create", new Domain.Models.Article()); %>
That render correctly. But when I hit the create button, it's doesn't go in the Create[post] method of my ArticleController.
Any idea why and how I could resolve that issue ?

If you have problems with the button, it's not going to have anything to do with how you're rendering the user control. We need to see the form markup that the button is inside, that will show what the problem is most likely.
But just for reference, you're probably looking to do something like this:
<% using (Html.BeginForm("Create",
ViewContext.RouteData.Values["Controller"].ToString())) { %>
your control markup here
<% } %>

Related

Display selection of index in a partial

I've been trying to solve the following challenge all day without any luck.
When going through forum posts I came across jQuery and AJAX which are both new concepts to me and which I'd rather skip for now, if possible.
I've got a partial, "navbar-left", which shows a list of all bank accounts in my model Account.
When the user clicks on one of the items in the list, all transactions of that account should be shown in the same page at the right. The partial below links to a new page which is not how I'd like it.
The navbar-partial:
<ul class="nav nav-pills nav-stacked">
<% #accounts.each do |account| %>
<li role="presentation"><%= link_to account.account_holder, account_mutations_path(account.id) %></li>
<% end %>
</ul>
Any tips on how to get this fixed is much appreciated!
The page with the navbar at the left
The mutations in a separate page instead of a partial
Either you're sending viewers to a new page, or dynamically loading content within their current page.
If the latter, then the only solution is AJAX.
Luckily, Ruby on Rails makes transitioning from one to the other very easy.
Here is a gist of how it works:
<%= link_to account.account_holder, account_mutations_path(account.id), remote: true %>
This was pointing back to some page previously (e.g. action.html.erb).
Because of remote: true, it's going to be sending JS directly to the browser instead of a new HTML page (e.g. action.js.erb in the same view folder and same action name).
Here we can control the behavior we want by rendering a partial using ERB and using JS to change the HTML content of some part of the page:
// action.js.erb
$('#some_element').html('<%= j render "partial" %>')
Which will insert the HTML of the partial directly into the JQuery that changes the content dynamically.
Where j is a shorthand for escape_javascript.
Without escaping, the Ruby output is interpreted as file output and newlines would break your JS.
Example JS output without escaping:
// Bad
$('#some_element').html('<span>Content</span>
<span>More Content</span>')
Example with escaping:
// Good
$('#some_element').html('<span>Content</span>\n<span>More Content</span>')
http://guides.rubyonrails.org/working_with_javascript_in_rails.html
https://launchschool.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails
There are more great examples online and even Railscasts.
Really AJAX is the best way to do this, and it's not as complicated as you might think. But if you really want to skip AJAX then your best approach is probably to load ALL transactions for all accounts, in different div's and then show or hide them based on which is clicked.
For a rudimentary introduction to this look at javascript tabs... you click on a tab, the appropriate information is shown.
http://www.w3schools.com/howto/howto_js_tabs.asp
You can do this very simply without ajax. The big difference would be - it's not the same page. One page would be the account#index (as you have now), the other page is the account#show page.
For the show page, use a very similar view as the index page, the left side would include the partial with one of the account li class="active" to highlight the account you are currently on. For the right side of the page, render the account mutations list items.

Keeping track of which view a partial is called from

I call the same partial from multiple views in Rails, the show page and the edit page. How can I keep track of which view the partial is called from? More specifically, I would like to adapt the partial slightly depending on which page it is rendered from. I have tried to request the uri using url_for(:only_path => true) and if-else statements to determine if the partial is rendered on the show or edit page, but this is a bit cumbersome. Is there a better approach?
you can try to use current_page? helper, i.e. if current_page?(root_path)
We put this in our application.html.erb template which shows the current controller, view and more debug information:
<%= debug(params) if Rails.env.development? %>
I think we got this from the Rails site:
http://guides.rubyonrails.org/debugging_rails_applications.html

RoR- How to use div onclick to render results in a separate area of the page?

I am learning Ruby on Rails, and I am very confused on how the controller-model-view relationship works for my application.
What I have now is a table full of comments (posts) users have made. What I want to do is let users click on a comment to see more information in a separate panel (ie, other database fields that weren't initially shown, for example the user_id of the person who posted the comment).
In my _post.html.erb, I have something like:
<div class="post" id="<%= post.post_id %>" onclick = ?? >
<p>post.text</p></div>
What should go in onclick? I need a way for the onclick to call a helper/controller method which can load more information, and then put that in another div on a page (I've tried variations of using the controller and helper to call javascript which inserts html into the site, but that seems messier than it should be). From what I understand, I should create some kind of partial _postdetails.html.erb file that handles the actual displaying of the html, but I have no idea how to specific where that partial would go in the page.
Any help would be appreciated, thanks!
You can achieve what you want either by using Rails helpers or by writing the AJAX calls yourself.
Personally I manually write all my AJAX calls using jQuery.
You can also use Prototype which ships with Rails.
That being said you can do.
In your JS file :
$("div.some-class").click(function()
{
$.ajax(
{
url:"url/to/controller/action",
type:<GET>/<POST>,
data://If you wish to sent any payload
});
});
In your controller :
def some_action
#some computation
render :update do |page|
page["id_of_div_to_be_refreshed"].replace_html :partial => "some_partial"
end
end

Rails + facebox + authlogic - how?

on my web site I want to have login/registration form in modal window done using facebox (jQuery plugin). What is better:
Create view with one method and template that has form and refer facebox to this view.
Create static HTML file in public directory and refer facebox to this static page.
What I want to achieve is:
Easy verification (like "user name already taken", "password confirmation doesn't match password" and stuff like that).
Easy submit and redirect
I'm new to Rails, I just know about forms verification in Django, so for Django I would probably choose option 1, but it might be another thing in Ruby.
If you want the verification to come back to the registration page, you should make it a dynamic page.
The other problem with a static page in the public directory is that your links all become hardcoded, so if you application ever lives off the domain root (i.e. example.com/app) the links in that static file could be wrong.
Additionally, if you ever need to move your images to a different host, you lose the advantages of the image_tag.
Only use static resources if you know things won't change and you need speed. If your dynamic pages are too slow, you can cache them, or you might be doing something wrong.
UPDATE: (to address the first comment)
You can't use the rails functions to build your URLs when you are in the public folder. If you need rails generated URLs in your javascript, trigger them from a rails view page.
Generally, I'll do the following:
In application.html.erb in the head tag:
<%= yield :headScripting %>
Then in the view page that is triggering the javascript:
<% content_for :headScripting do %>
jQuery().ready(function() {
jQuery("#placeholder").load("<%= summary_model_path(#model) %>");
});
<% end %>
That would load the summary text from the model controller action summary. This would probably render :text => "summary" or render :layout => false depending on your needs

can link_to lead to rendering sth?

i want to render a partial within a view. so that when button MORE is clicked everything stays the same just additional characters are shown. in my case the whole article.
<%= #article1.content[0..300] + "..." %>
<%= link_to "more", ....... %>
i dont know what the right methot would be. somehow i have to explain to rails that when button more is clicked it shows me the whole article. maybe i shouldn't use method link_to ..
thank you in advance for your replys
What you're looking for is link_to_remote or link_to_function.
link_to_remote will be fetching the rest of the article from your controller and replacing/appending to a DOM element with a partial via RJS. This allows you to minimize unnecessary data being sent, and facilitates handling users that have javascript disabled.
With link_to_function, the entire article will be served when the page is loaded, but the everything beyond the first 300 characters will be hidden by CSS. This is easier to set up but sends a lot more data, it also relies on the user having javascript enabled.
Without looking at the source the average user probably couldn't distinguish between the two methods.
Which choice you go with is up to you. Sorry, I haven't got time to provide code examples, but the internet is full of them.
try link_to_function, use truncate for part and insert hidden tag with full text, switch them using javascript in link_to_function

Resources