I built a multistep form with wicked-gem consisting of three steps.
The last step should be callable 1 to x times.
So I added another button to my form:
if current_step?(:add_principal_claim)
= file.submit value: 'next_claim', class: 'button tiny radius'
= link_to 'finish', Wicked::FINISH_STEP, method: :put, class: 'button tiny radius'
and add another step in my controller
steps :add_file_header, :add_claim_header, :add_principal_claim, :next_principal_claim
def show
if step == :add_claim_header
case step
when :next_principal_claim
redirect_to wizard_path(:add_principal_claim)
else
render_wizard
end
end
end
The last needed step is :add_principal_claim. If neccessary it should be called several times to store more than one dataset to the model.
Calling the form from the previuos step leads into show action and renders the add_principal_claim view, clicking the file.submit button leads to the update action in the controller, stores the dataset into the model and recalls the add_principal_claim view as intended.
How can I get the link_to button to jump into update action, store the dataset and then finally jump out of the wizard?
Any suggestion would be appreciated.
For those with the same problem: I added another step called :finish and in the form I now have two submit buttons with different values of course, here called 'next_principal_claim' and 'finish'.
def show
if params[:commit] == next_principal_claim
redirect_to wizard_path(:add_principal_claim)
elsif params[:commit] == finish
redirect_to project_path(#project)
else
render_wizard
end
end
That works fine for me!
Related
I have Custom page "All Posts"
Can i integrate batch action into this custom page for assigning the post to a particular category
ActiveAdmin.register_page "All Posts" do
menu :priority => 1#, label: proc{ I18n.t("active_admin.dashboard") }
content do #title: proc{ I18n.t("active_admin.dashboard") }
#how can i put here a batch action
end
end
how can i put here a batch action as it gives errors when i write the batch action for resource code.
should i write the page_action and then customize it using javascript and partials.
Thanks in advance
You can try to add batch_action by
batch_action :export do |selection|
keys = Model.find()
redirect_to admin_path_to_page_with_category_selection_path(post_ids: selection)
end
and selectable column
index download_links: [:xlsx] do
selectable_column
.....
end
admin_path_to_page_with_category_selection_path goes to view with category combobox, selected posts and submit button
To learn rails, I started to write my own (stripped down) version of reddit. Currently, I have my comments routes nested inside my post routes as such:
resources :posts do
resources :comments
end
For my comments controller, under index & create I have the following
def index
#post = Post.find(params[:post_id])
#comments = #post.comments
if params[:comment].nil?
#comment = Comment.new
#comment.post_id = #post.id
else
#comment = Comment.find([:comment])
end
end
def create
#comment = Comment.new(comment_params)
#comment.user_id = current_user.id
#comment.post_id = params[:post_id]
if #comment.save
#flash
redirect_to post_path(#comment.post)
else
#flash
render 'index'
end
end
Which works well, except the last part: render
I want my comments to display on the same page as the other comments (just like reddit), so I would prefer not use the www.example.com/post/4/comment/new path, and instead do it through the www.example.com/post/4/comments path.
I understand that I can do a redirect, however I want to keep the comment text, so the user can make corrections. Is there a way to properly do this with a render, as opposed to me putting the text in a session variable and doing a redirect? I understand this is an edge case, but am trying to use this as a learning opportunity.
You can pass the comment to the index partial - no?
<%= render "index", locals: {comment: #comment} %>
Or try a partial;
<%= render partial: "index", locals: {comment: #comment} %>
I am new to rails and am looking to do something very simple but don't know the correct syntax. I have 2 buttons one for creating a new page and one for creating a new post. Both a page and a post save in the same database table and are controlled through a boolean field called 'static'. A page therefore has a static value of 1 and a post 0. What I want to do is simply auto set this value in a form (and hide it) when I click new page or post. I imagined the link to create a new page would work something like this:
<%= link_to 'New Page', new_page_path(:static => "1") %>
This doesn't work so I tried to create a new_static page action and a new_post page action with correcting routing (for displaying only pages I created a show_static action used the following link_to and it works fine):
<%= link_to "Pages", show_static_pages_path(#pages), :method => :get %>
The problem is when I created the new_static page action it expects an id for some reason.
new_static_page GET /pages/:id/new_static(.:format) pages#new_static
I would prefer to not mess around with new actions and routing and simply set the value with link_to. Any tips would be greatly appreciated.
I'm at a total loss, I've researched as much as I can and cannot find the answer. Here is the code below:
def create
#budget_source = BudgetSource.create(budget_source_params)
#budget_source.update(:user_id => current_user.id)
#budget_source.budget_segments.order(:id).each do |bs|
bs.budget_ratios.order(:id).each_with_index do |br, i|
br.update(scenario_id: i+1)
end
end
render 'show'
end
A new BudgetSource is created every time I refresh the page.
The problem is with render 'show'. When you submit the form, you are staying on the same page since render just renders the template. So when you refresh the page, instead of calling the show method, it actually calls the create method since the last action you did was a POST instead of a GET. What you should do instead is to do a redirect back to the show method with something like redirect_to #budget_source. Most browsers usually warn if you are refreshing a page which would cause a POST method instead of a GET.
I'm a rails beginner learning rails 4 and I'm trying to learn by doing. I'm making a simple blog that I want some simple user authentication on. I'm trying to learn here, so I don't want to implement Devise, etc. I have a header partial that takes care of my site header and I'm trying to put a link to logout that only shows if a user is logged in. I have a simple session controller that has a new action for the signup form, a create action that sets the current user after matching the email and password and sets session[:user_id] = #current_user.id, and a destroy action that nils out the session. In my application controller I have a method like this
def logged_in?
!session[:user_id].nil?
end
In my _header.html.erb partial, I have
<% if logged_in? %>
(My link)
<% end %>
When I load the page it tells me it can't find the "logged_in?" method. Anyone know why? Thanks.
Methods on controllers are by default not exposed to the views (which your partial is part of).
2 solutions:
Create your logged_in? method as a helper method, for example in an AuthenticationHelper. Doing this you cannot access it from controllers, though.
Expose your controller method to the view using helper_method:
class ApplicationController < ActionController::Base
helper_method :logged_in?
def logged_in?
[...]
end
end
Couldn't you just use:
if #current_user