1

I'm a student intern and I have to improve the website of the company. I created a partial in Ruby on Rails, and I want to use it multiple times on the same page. I have some nav tabs and depending on the active tab, the same partial should be displayed with different parameters. What I wanted to do is to re render my partial when the user changes the active tab. So I searched on the net how to re render a partial using rails and js, and it seems that I have to use ajax (with respond_to in rails). So I did it and somewhere else on the website there is a similar thing, but it didn't work for me and I'm pretty sure, it's a routing problem, because on the website it's done on a specific page (beneficiaries/1 for instance), but I have to do it on stories/new

Here is the link which call the function to re render the code :

= link_to 'Activities', modify_tab_new_project_story_path(@project), method: :post, remote: true, :style => 'text-align: center;'

Then in the routes.rb file : (I tried a lot of different things but none of them worked)

resources :projects, only: [:show, :destroy] do
  resources :stories do
     post :modify_tab, on: :new
  end
end

My controller :

def change_tab
    respond_to do |format|
      format.js
    end
  end

And finally I'm redirected to :

http://localhost:3000/projects/issue-1004/stories/new/modify_tab

instead of staying in the same page.

In addition I get this error

whereas I use the method post.

It's really confused in my brain so I probably made some mistakes.

the thing which works on the website :

Here is the view :

= form_tag create_note_charity_beneficiaries_path(@charity), method: :post, remote: true, id: 'note_form' do

Then the routes.rb :

resources :charities, except: :index do
    resources :beneficiaries do
      collection do
        post :create_note
      end
    end
  end

And finally the controller :

def create_note
    note = Note.create(params[:note])
    @beneficiary = note.beneficiary
    respond_to do |format|   
      format.js
    end
  end

There is also a create_note.js.erb file and I've created one too, but the problem come before this file so I didn't put it here.

Hope someone could help me

Thanks

Vivien
  • 11
  • 2
  • Do `rails c` and try `helper.link_to "Post", "/", :method => :post` and print the HTML output here. – Pavan Jul 04 '17 at 13:04
  • Your `routes.rb` file seems to be incorrect, `post :modify_tab, on: :new` usually you can only define routes on member or collection. https://stackoverflow.com/questions/3028653/difference-between-collection-route-and-member-route-in-ruby-on-rails Can you edit your `routes.rb` file accordingly and then post the result of the `rails routes` command ? – Aschen Jul 04 '17 at 13:19
  • Here is the result of the command : "Post" – Vivien Jul 04 '17 at 13:44
  • Aschen, I agree with you, I think the error must be linked to the routes file, but I ever tried with member but it didn't work I think, but let me try again. What's more, I've seen that we can add routes on :new, on this website : http://guides.rubyonrails.org/routing.html, 2.10.3 Adding Routes for Additional New Actions – Vivien Jul 04 '17 at 13:48
  • When I try to modify to on: member, I get a routing error before clicking on the button. I've modified the routes file as : `post :modify_tab, on: :member` and the link as `= link_to 'Activities', modify_tab_project_story_path(@project), method: :post, remote: true, :style => 'text-align: center;'` – Vivien Jul 04 '17 at 13:53
  • And when I run `rake routes`, I can see this line : `modify_tab_project_story POST /projects/:project_id/stories/:id/modify_tab(.:format) stories#modify_tab` – Vivien Jul 04 '17 at 14:04
  • You should use the route `modify_tab_project_story_path` with passing your project and story model like this in order to work : `modify_tab_project_story_path(@project, @story)` – Aschen Jul 04 '17 at 14:49
  • Doesn't resolve my issue, I still get this error : `Routing Error No route matches {:action=>"modify_tab", :controller=>"stories", :project_id=># – Vivien Jul 04 '17 at 15:34
  • I found the solution, I made a form inside an other form, that's why there were some conflicts. Now the forms from jquery which allow the usage of ajax command can be used. – Vivien Jul 18 '17 at 13:36

0 Answers0